千家信息网

XmlDocument对象操作的示例分析

发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,这篇文章主要介绍XmlDocument对象操作的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!使用XmlReader遍历文档是很方便的,使用XmlWriter生成一个新
千家信息网最后更新 2024年10月19日XmlDocument对象操作的示例分析

这篇文章主要介绍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 attributesnewBook.SetAttribute("genre", "Mystery");      newBook.SetAttribute("publicationdate", "2001");      newBook.SetAttribute("ISBN", "123456789");//create a new 'title' elementXmlElement newTitle = _doc.CreateElement("title");      newTitle.InnerText = "Case of the Missing Cookie";      newBook.AppendChild(newTitle); //create new author elementXmlElement 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对象操作的示例分析"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

节点 属性 文档 对象 内容 文件 类型 示例 分析 方法 篇文章 一致 价值 位置 元素 兴趣 小伙 小伙伴 文本 更多 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 宜宾网络技术专业 新建gpo服务器上没有注册表值 数据库实验中如何修改数据库名 太仓做软件开发的公司 小学生网络安全预防教材 两个服务器互传数据 宁波计算机网络技术包括什么 万方数据库全文收录 新华服务器位置 派出所宣传网络安全法简报 网络安全应急办设在哪里 市妇联网络安全宣传 oracle数据库自学咋样 php怎么搭建数据库 cod16找不到服务器错误代码 腾讯云云服务器怎么添加端口 朔州网络技术价格 用服务器做棋牌游戏 如果域名服务器出现问题 网约车项目 服务器可以使用阿里云吗 荔湾小程序软件开发哪家口碑好 企业用的服务器防火墙是关闭的吗 数据库名称怎样查看 南宁的软件开发公司 农业银行软件开发 天津 天津银行 软件开发中心 数据库创建表时属性类型自动编号 网络安全防止诈骗 联通软件开发岗面试经验 数据库 改变一列名称sql
0