C#操作XML方法集合

妖狐艹你老母 2022-06-04 03:51 250阅读 0赞

* 1 XMLElement 主要是针对节点的一些属性进行操作
* 2 XMLDocument 主要是针对节点的CUID操作
* 3 XMLNode 为抽象类,做为以上两类的基类,提供一些操作节点的方法

10232857-343f0a46e24149f889913580dca36af0.png

清楚了以上的关系在操作XML时会更清晰一点

二 具体操作(C#)

  以下会对Xml的结点与属性做增 删 改 查的操作也满足了实际工作中的大部分情况

先构造一棵XML树如下,其中也涉及到了写入xml文档的操作

复制代码

  1. 1 public void CreatXmlTree(string xmlPath)
  2. 2 {
  3. 3 XElement xElement = new XElement(
  4. 4 new XElement("BookStore",
  5. 5 new XElement("Book",
  6. 6 new XElement("Name", "C#入门", new XAttribute("BookName", "C#")),
  7. 7 new XElement("Author", "Martin", new XAttribute("Name", "Martin")),
  8. 8 new XElement("Adress", "上海"),
  9. 9 new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
  10. 10 ),
  11. 11 new XElement("Book",
  12. 12 new XElement("Name", "WCF入门", new XAttribute("BookName", "WCF")),
  13. 13 new XElement("Author", "Mary", new XAttribute("Name", "Mary")),
  14. 14 new XElement("Adress", "北京"),
  15. 15 new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
  16. 16 )
  17. 17 )
  18. 18 );
  19. 19
  20. 20 //需要指定编码格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常
  21. 21 XmlWriterSettings settings = new XmlWriterSettings();
  22. 22 settings.Encoding = new UTF8Encoding(false);
  23. 23 settings.Indent = true;
  24. 24 XmlWriter xw = XmlWriter.Create(xmlPath,settings);
  25. 25 xElement.Save(xw);
  26. 26 //写入文件
  27. 27 xw.Flush();
  28. 28 xw.Close();
  29. 29 }

复制代码

然后得到如下的XML树

复制代码

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <BookStore>
  3. 3 <Book>
  4. 4 <Name BookName="C#">C#入门</Name>
  5. 5 <Author Name="Martin">Martin</Author>
  6. 6 <Date>2013-10-11</Date>
  7. 7 <Adress>上海</Adress>
  8. 8 <Date>2013-10-11</Date>
  9. 9 </Book>
  10. 10 <Book>
  11. 11 <Name BookName="WCF">WCF入门</Name>
  12. 12 <Author Name="Mary">Mary</Author>
  13. 13 <Adress>北京</Adress>
  14. 14 <Date>2013-10-11</Date>
  15. 15 </Book>
  16. 16 </BookStore>

复制代码

以下操作都是对生成的XML树进行操作

2.1 新增节点与属性

新增节点NewBook并增加属性Name=”WPF”

复制代码

  1. 1 public void Create(string xmlPath)
  2. 2 {
  3. 3 XmlDocument xmlDoc = new XmlDocument();
  4. 4 xmlDoc.Load(xmlPath);
  5. 5
  6. 6 var root = xmlDoc.DocumentElement;//取到根结点
  7. 7 XmlNode newNode = xmlDoc.CreateNode("element", "NewBook", "");
  8. 8 newNode.InnerText = "WPF";
  9. 9
  10. 10 //添加为根元素的第一层子结点
  11. 11 root.AppendChild(newNode);
  12. 12 xmlDoc.Save(xmlPath);
  13. 13 }

复制代码

开篇有写操作xml节点属性主要用XmlElement对象所以取到结点后要转类型

复制代码

  1. 1 //属性
  2. 2 public void CreateAttribute(string xmlPath)
  3. 3 {
  4. 4 XmlDocument xmlDoc = new XmlDocument();
  5. 5 xmlDoc.Load(xmlPath);
  6. 6 var root = xmlDoc.DocumentElement;//取到根结点
  7. 7 XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
  8. 8 node.SetAttribute("Name", "WPF");
  9. 9 xmlDoc.Save(xmlPath);
  10. 10
  11. 11 }

复制代码

效果如下

11124603-3ed0af40bcf1467bb762c960a75b131e.png

2.2 删除节点与属性

复制代码

  1. 1 public void Delete(string xmlPath)
  2. 2 {
  3. 3 XmlDocument xmlDoc = new XmlDocument();
  4. 4 xmlDoc.Load(xmlPath);
  5. 5 var root = xmlDoc.DocumentElement;//取到根结点
  6. 6
  7. 7 var element = xmlDoc.SelectSingleNode("BookStore/NewBook");
  8. 8 root.RemoveChild(element);
  9. 9 xmlDoc.Save(xmlPath);
  10. 10 }

复制代码

删除属性

复制代码

  1. 1 public void DeleteAttribute(string xmlPath)
  2. 2 {
  3. 3 XmlDocument xmlDoc = new XmlDocument();
  4. 4 xmlDoc.Load(xmlPath);
  5. 5 XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
  6. 6 //移除指定属性
  7. 7 node.RemoveAttribute("Name");
  8. 8 //移除当前节点所有属性,不包括默认属性
  9. 9 //node.RemoveAllAttributes();
  10. 10 xmlDoc.Save(xmlPath);
  11. 11 }

复制代码

2.3 修改节点与属性

xml的节点默认是不允许修改的,本文也就不做处理了

修改属性代码如下

复制代码

  1. 1 public void ModifyAttribute(string xmlPath)
  2. 2 {
  3. 3 XmlDocument xmlDoc = new XmlDocument();
  4. 4 xmlDoc.Load(xmlPath);
  5. 5 XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
  6. 6 element.SetAttribute("Name", "Zhang");
  7. 7 xmlDoc.Save(xmlPath);
  8. 8 }

复制代码

效果如下

11125244-4a07e55bd9ed4d6caa7803f457f19597.png

2.4 获取节点与属性

复制代码

  1. 1 public void Select(string xmlPath)
  2. 2 {
  3. 3 XmlDocument xmlDoc = new XmlDocument();
  4. 4 xmlDoc.Load(xmlPath);
  5. 5 //取根结点
  6. 6 var root = xmlDoc.DocumentElement;//取到根结点
  7. 7 //取指定的单个结点
  8. 8 XmlNode oldChild = xmlDoc.SelectSingleNode("BookStore/NewBook");
  9. 9
  10. 10 //取指定的结点的集合
  11. 11 XmlNodeList nodes = xmlDoc.SelectNodes("BookStore/NewBook");
  12. 12
  13. 13 //取到所有的xml结点
  14. 14 XmlNodeList nodelist = xmlDoc.GetElementsByTagName("*");
  15. 15 }

复制代码

属性

复制代码

  1. 1 public void SelectAttribute(string xmlPath)
  2. 2 {
  3. 3 XmlDocument xmlDoc = new XmlDocument();
  4. 4 xmlDoc.Load(xmlPath);
  5. 5 XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
  6. 6 string name = element.GetAttribute("Name");
  7. 7 Console.WriteLine(name);
  8. 8 }

复制代码

三 具体操作 (linq to XML)

Linq to Xml 也没什么变化只操作对象改变了主要涉及的几个对象如下 注:我并没有用linq的语法去操作元素。

XDocument:用于创建一个XML实例文档

XElement:用于一些节点与节点属性的基本操作

以下是对Xml的 一些简单的操作

3.1 新增节点与属性

复制代码

  1. 1 public void Create(string xmlPath)
  2. 2 {
  3. 3 XDocument xDoc = XDocument.Load(xmlPath);
  4. 4 XElement xElement = xDoc.Element("BookStore");
  5. 5 xElement.Add(new XElement("Test", new XAttribute("Name", "Zery")));
  6. 6 xDoc.Save(xmlPath);
  7. 7 }

复制代码

属性

复制代码

  1. 1 public void CreateAttribute(string xmlPath)
  2. 2 {
  3. 3 XDocument xDoc = XDocument.Load(xmlPath);
  4. 4 IEnumerable<XElement> xElement = xDoc.Element("BookStore").Elements("Book");
  5. 5 foreach (var element in xElement)
  6. 6 {
  7. 7 element.SetAttributeValue("Name", "Zery");
  8. 8 }
  9. 9 xDoc.Save(xmlPath);
  10. 10 }

复制代码

3.2 删除节点与属性

复制代码

  1. 1 public void Delete(string xmlPath)
  2. 2 {
  3. 3 XDocument xDoc = XDocument.Load(xmlPath);
  4. 4 XElement element = (XElement)xDoc.Element("BookStore").Element("Book");
  5. 5 element.Remove();
  6. 6 xDoc.Save(xmlPath);
  7. 7 }

复制代码

属性

复制代码

  1. 1 public void DeleteAttribute(string xmlPath)
  2. 2 {
  3. 3 XDocument xDoc = XDocument.Load(xmlPath);
  4. 4 //不能跨级取节点
  5. 5 XElement element = xDoc.Element("BookStore").Element("Book").Element("Name");
  6. 6 element.Attribute("BookName").Remove();
  7. 7 xDoc.Save(xmlPath);
  8. 8 }

复制代码

3.3 修改节点属性

节点.net没提供修改的方法本文也不做处理

修改属性与新增实质是同一个方法

复制代码

  1. 1 public void ModifyAttribute(string xmlPath)
  2. 2 {
  3. 3 XDocument xDoc = XDocument.Load(xmlPath);
  4. 4 XElement element = xDoc.Element("BookStore").Element("Book");
  5. 5 element.SetAttributeValue("BookName","ZeryTest");
  6. 6 xDoc.Save(xmlPath);
  7. 7 }

复制代码

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读