1 创建XML文档
使用XmlDocument类型创建XML文档
XmlDocument doc = new XmlDocument();
2 将XML文档读入DOM
2.1 可使用XmlDoucment.Load方法的不同重载将文档置入内存
2.2 可使用XmlDoucment.LoadXml方法从字符串中读取XML
举例:
XmlDoucment doc = new XmlDoucment();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
doc.Save("data.xmls");
从读取器中加载数据
XmlDoucment doc = new XmlDoucment();
XmlReaderSettings setting = new XmlReaderSettings();
setting.IgnoreWhiteSpace = true ;
XmlReader reader = XmlReader.Create("bookstore.xml", setting);
reader.MoverTocontent();
reader.Read();
reader.Skip();
reader.Skip();
doc.Load(reader);
3 检索单个属性节点
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlElement root = doc.DocumetElement;
XmlAttribute attr = root.GetAttributeNode("ISBN");
String attriValue = attr.InnerXml;
4创建并添加注释
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
//创建注释
XmlComment newComment
newComment = doc.CreatComment("sample XML document");
//把这个注释加进去
XmlElement root = doc.DocumentElement;
doc.InsertBefore(newComment, root);
5 创建并添加元素
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlElement elem = doc.CreatElement("price");
XmlText text = doc.CreateTextNode("19.32"); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
doc.DocumentElement.AppendChild(elem); //将该节点添加到给定节点(此处是book 节点)的子节点列表的末尾
doc.DoucmentElement.lastChild.AppendChild(text);.
6使用SetAttribute
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmllns:bk='urn:samples' bk"ISBN= '1-33'>" +"<title>Pride and Prejudice</title>"+"</book>");
XmlElement root = doc.DocumentElement;
root.SetAttribute("genre","urn:samples","novel");
7使用CreatAttribute
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmllns:bk='urn:samples' bk"ISBN= '1-33'>" +"<title>Pride and Prejudice</title>"+"</book>");
XmlAttribute attr = doc.CreatAttribute("publisher");
attr.Value = "worldWilePulishing";
doc.DocumentElement.SetAttributeNode(attr);
例子6 和7 是创建新属性和添加的过程
方法6 获取元素节点并使用SetAttribute将属性添加到该元素的属性集合
方法7 使用CreatAttribute方法创建XmlAttribute节点,获取元素节点,然后使用SetAttributeNod方法添加
8 从DOM中移除节点
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmllns:bk='urn:samples' bk"ISBN= '1-33'>" +"<title>Pride and Prejudice</title>"+"</book>");
XmlNode root = doc.DocumentElement;
//把节点title移除
root.RemoveChild(root.FirstChild);
//移除所有的节点和属性
root.RemoveAll();
9 修改XML文档中的节点,内容和值
9.1 使用Value
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlElement myElement = doc.DocumentElement;
//获取book 节点的所有属性
XmlAttributeCollection attrColl = myElement.Attributes;
//更改属性值利用Value
attrColl[0].Value= "poetry";
9.2使用ReplaceChild
XmlDoucment doc = new xmlDocument();
doc.LoadXml("<book genre = 'novel' ISBN = '1-861001-57-3'>" + "<title> Pride And Prejudice</title>" + "</book>");
XmlNode root = doc.DocumentElement;
XmlElement elem = doc.CreatElement("title");
elem.InnerText = "The Handmaid tale"; //注意和上面带@@@@@的区别和联系
//把title节点给替换了
root.ReplaceChild(elem,rooot.FirstChild);
10 保存文档
用OUtetXml属性
Xmldocument mydoc = new xmlDocument();
string xml= mydoc.OuterXml;