XmlDocument 对象操纵细致步骤【XML教程】,XmlDocument
作者:搜教程发布时间:2019-11-30分类:XML教程浏览:35评论:0
导读:运用XmlReader遍历文档是很轻易的,运用XmlWriter生成一个新的XML文档也很轻易.然则对现有的XML举行修正,比方增加一个元素或修正一个属性值,就比较麻烦了.此时,能...
运用XmlReader遍历文档是很轻易的,运用XmlWriter生成一个新的XML文档也很轻易.然则对现有的XML举行修正,比方增加一个元素或修正一个属性值,就比较麻烦了.此时,能够运用XmlDocument对象,经由过程挪用DOM要领对文档举行修正,然后再保留.因为DOM已举行了标准化,许多言语都对他举行了支撑,比方JS,因而这里的许多要领与JS中都是一致的,比方GetElementByID(),GetElementByTagName(),AppendChild(),InsertAfter()等.
1、建立一个xml文件
XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); XmlElement root = doc.CreateElement("bookstore");//建立根节点 doc.AppendChild(root); XmlElement newBook = _doc.CreateElement("book");//create a new 'book' element //set some attributes newBook.SetAttribute("genre", "Mystery"); newBook.SetAttribute("publicationdate", "2001"); newBook.SetAttribute("ISBN", "123456789"); //create a new 'title' element XmlElement newTitle = _doc.CreateElement("title"); newTitle.InnerText = "Case of the Missing Cookie"; newBook.AppendChild(newTitle); //create new author element XmlElement newAuthor = _doc.CreateElement("author"); newBook.AppendChild(newAuthor); //create new name element XmlElement newName = _doc.CreateElement("name"); newName.InnerText = "Cookie Monster"; newAuthor.AppendChild(newName); //create new price element XmlElement newPrice = _doc.CreateElement("price"); newPrice.InnerText = "9.95"; newBook.AppendChild(newPrice); //add to the current documentdoc.DocumentElement.AppendChild(newBook);//_doc.DocumentElement为猎取xml的根 doc.Save("bb.xml");将 XML 文档保留到指定的位置
2、插进去节点 与建立xml 文件相似
_doc.Load("books.xml"); XmlElement newBook = _doc.CreateElement("book"); newBook.SetAttribute("genre", "Mystery"); newBook.SetAttribute("publicationdate", "2001"); newBook.SetAttribute("ISBN", "123456789"); XmlElement newTitle = _doc.CreateElement("title"); newTitle.InnerText = "Case of the Missing Cookie"; newBook.AppendChild(newTitle); XmlElement newAuthor = _doc.CreateElement("author"); newBook.AppendChild(newAuthor); XmlElement newName = _doc.CreateElement("name"); newName.InnerText = "Cookie Monster"; newAuthor.AppendChild(newName); XmlElement newPrice = _doc.CreateElement("price"); newPrice.InnerText = "9.95"; newBook.AppendChild(newPrice); _doc.DocumentElement.AppendChild(newBook); _doc.Save("booksEdit.xml"); 或许下面如许保留 XmlTextWriter tr = new XmlTextWriter("booksEdit.xml", null);//将xml文档保留,假如存在此文件,则掩盖 tr.Formatting = Formatting.Indented; _doc.WriteContentTo(tr); tr.Close();
3、修正xml节点
将genre属性值为“novel“的节点的genre值改成“updatenovel”,将该节点的子节点的文本修正为“啦啦啦啦”。
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//猎取bookstore节点的一切子节点 foreach(XmlNode xn in nodeList)//遍历一切子节点 { XmlElement xe=(XmlElement)xn;//将子节点范例转换为XmlElement范例 if(xe.GetAttribute("genre")=="novel")//假如genre属性值为“李赞红” { xe.SetAttribute("genre","updatenovel");//则修正该属性为“update李赞红” XmlNodeList nls=xe.ChildNodes;//继承猎取xe子节点的一切子节点 foreach(XmlNode xn1 in nls)//遍历 { XmlElement xe2=(XmlElement)xn1;//转换范例 if(xe2.Name=="title")//假如找到 { xe2.InnerText="亚胜";//则修正 break;//找到退出来就能够了 } } break; } } xmlDoc.Save("bookstore.xml");//保留。
4、删除节点
节点的genre属性,删除 节点。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes; foreach(XmlNode xn in xnl) { XmlElement xe=(XmlElement)xn; if(xe.GetAttribute("genre")=="fantasy") { xe.RemoveAttribute("genre");//删除genre属性 } else if(xe.GetAttribute("genre")=="update李赞红") { xe.RemoveAll();//删除该节点的全部内容 } } xmlDoc.Save("bookstore.xml");
5、遍历xml
string filePath = "bookstore.xml"; XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlNode root = doc.DocumentElement; showNode(root); private static void showNode(XmlNode root) { if (root.NodeType==XmlNodeType.Text) { Console.WriteLine(root.Value); } if (root.NodeType==XmlNodeType.Element) { Console.WriteLine(root.Name); } if (root.Attributes!=null&&root.Attributes.Count>0) { foreach (XmlAttribute attr in root.Attributes) { Console.Write("{0}={1} ",attr.Name,attr.Value); } Console.WriteLine(); } XmlNodeList chiledList = root.ChildNodes; foreach (XmlNode child in chiledList) { showNode(child); } }
以上就是XmlDocument 对象操纵细致步骤的细致内容,更多请关注ki4网别的相干文章!
标签:XmlDocument
相关推荐
- XmlDocument XML编码转换的示例代码分享【XML教程】,XmlDocument ,XML,编码转换
- XMLTextReader和XmlDocument读取XML文件的比较【XML教程】,XMLTextReader,XmlDocument,XML文件
- XmlDocument XML编码转换的示例代码分享【XML教程】,XmlDocument ,XML,编码转换
- XmlDocument操纵xml文档的示例代码【XML教程】,XmlDocument,xml
- 详解经由过程XmlDocument读写Xml文档的示例代码【XML教程】,XmlDocument,Xml
- XMLTextReader和XmlDocument读取XML文件的比较【XML教程】,XMLTextReader,XmlDocument,XML文件
你 发表评论:
欢迎- XML教程排行
-
- 1您相识XML么?它是做什么用的?【XML教程】,XML
- 2XML中的定名空间的示例代码详解【XML教程】,XML,命名空间
- 3怎样在Python中不换行的输出【XML教程】,Python,输出
- 4Java中剖析XML的体式格局有哪些【XML教程】,Java,解析XML
- 5为什么用json不必xml【XML教程】,json,xml
- 6有关XML剖析中DOM剖析的细致引见【XML教程】,DOM
- 7完成Asp与XML交互的实例剖析【XML教程】,Asp,XML,交互
- 8XQuery是什么【XML教程】,XQuery,xml
- 9运用XSLT将XML数据转换成HTML【XML教程】,XSLT,XML,HTML
- 最新文章
- 广而告之