XmlDocument 对象操作详细步骤

发布时间:2022-05-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了XmlDocument 对象操作详细步骤脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
使用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 对象操作详细步骤的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的XmlDocument 对象操作详细步骤全部内容,希望文章能够帮你解决XmlDocument 对象操作详细步骤所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: