zoukankan      html  css  js  c++  java
  • C# 使用XmlDocument类对XML文档进行操作

    原创地址:http://www.cnblogs.com/jfzhu/archive/2012/11/19/2778098.html

    转载请注明出处

    W3C制定了XML DOM标准。很多编程语言中多提供了支持W3C XML DOM标准的API。我在之前的文章中介绍过如何使用Javascript对XML文档进行加载与查询。在本文中,我来介绍一下.Net中的XmlDocument类。它支持并扩展了W3C XML DOM标准。它将整个XML文档都先装载进内存中,然后再对XML文档进行操作,所以如果XML文档内容过大,不建议使用XmlDocument类,因为会消耗过多内存。对于很大的XML文档,可以使用XmlReader类来读取。因为XmlReader使用Steam(流)来读取文件,所以不会对内存造成太大的消耗。下面就来看一下如何使用XmlDocument类。

    (一) 加载

    加载XML比较常用的有三种方法:

    public virtual void Load(string filename);
    
    public virtual void Load(Stream inStream);
    
    public virtual void LoadXml(string xml);

    下面代码演示如何使用它们:

    复制代码
    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load("XMLFile1.xml"); 
    
    Entity retrievedAnnotation = _orgService.Retrieve("annotation" 
        , new Guid("C1B13C7F-F430-E211-8FA1-984BE1731399"), new ColumnSet(true));                
    byte[] fileContent = Convert.FromBase64String(retrievedAnnotation["documentbody"].ToString()); 
    MemoryStream ms = new MemoryStream(fileContent); 
    XmlDocument xmlDoc2 = new XmlDocument(); 
    xmlDoc2.Load(ms);
    
    string str = @"<Customers><Customer id='01' city='Beijing' country='China' name='Lenovo'/></Customers>"; 
    XmlDocument xmlDoc3 = new XmlDocument(); 
    xmlDoc3.LoadXml(str);
    复制代码

    (二) 查询

    对XML的元素、属性、文本的查询可以使用XPath。具体的定义可以参看w3school。

    首先应该了解一下XPath表达式:

    表达式 描述
    nodename 选取此节点的所有子节点。
    / 从根节点选取。
    // 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
    . 选取当前节点。
    .. 选取当前节点的父节点。
    @ 选取属性。

    我们主要使用两个方法来查询XML文档,SelectNodes(xpath expression)和SelectSingleNode(xpath expression)。

    SelectNodes返回一个XmlNodeList对象,也就是所有符合xpath表达式的xml节点都将会被返回,你需要对返回的结果进行遍历。

    SelectSingleNode只返回第一个符合xpath表达式的节点,或者返回null。

    以下面的XML文件为例,我们进行一些演示:

    复制代码
    <?xml version="1.0" encoding="utf-8" ?> 
    <Customers> 
      <Customer id="01" city="Beijing" country="China" name="Lenovo"> 
        <Contact gender="female" title="Support">Li Li</Contact> 
      </Customer> 
      <Customer id="02" city="Amsterdam" country="The Netherlands" name="Shell"> 
        <Contact gender="male" title="Sales Person">Aaron Babbitt</Contact> 
        <Contact gender="female" title="Sales Manager">Daisy Cabell</Contact> 
        <Contact gender="male" title="Sales Person">Gabriel Eads</Contact> 
      </Customer> 
    </Customers>
    复制代码

    1. 返回所有Contact节点:  

    XmlNodeList nodelist = xmlDoc.SelectNodes("/Customers/Customer/Contact"); 
    foreach (XmlNode node in nodelist) 
    { 
        Console.WriteLine(node.OuterXml); 
    } 

    输出结果为:

    <Contact gender="female" title="Support">Li Li</Contact>
    <Contact gender="male" title="Sales Person">Aaron Babbitt</Contact>   
    <Contact gender="female" title="Sales Manager">Daisy Cabell</Contact>   
    <Contact gender="male" title="Sales Person">Gabriel Eads</Contact>

    2. 返回id为02的customer:  

    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer[@id='02']"); 
    Console.WriteLine(node.OuterXml); 

    输出结果为:

    <Customer id="02" city="Amsterdam" country="The Netherlands" name="Shell">
        <Contact gender="male" title="Sales Person">Aaron Babbitt</Contact>
        <Contact gender="female" title="Sales Manager">Daisy Cabell</Contact>
        <Contact gender="male" title="Sales Person">Gabriel Eads</Contact>
    </Customer>

    3. 返回含有contact名为Li Li的contact:  

    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer/Contact[text()='Li Li']"); 
    Console.WriteLine(node.OuterXml); 

    输出结果:

    <Contact gender="female" title="Support">Li Li</Contact>

    4. 返回含有contact名为 Li Li 的customer。注意和3的区别:  

    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer[Contact/text()='Li Li']"); 
    Console.WriteLine(node.OuterXml);

    输出结果:

    <Customer id="01" city="Beijing" country="China" name="Lenovo">
        <Contact gender="female" title="Support">Li Li</Contact>
    </Customer>

    5. (1) 获取outer xml:
    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer[@id='02']");
    Console.WriteLine(node.OuterXml);

    (2) 获取 inner xml:
    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer[@id='02']");
    Console.WriteLine(node.InnerXml);

    (3) 获取 text
    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer/Contact[text()='Li Li']");
    Console.WriteLine(node.InnerText);

    (4) 获取属性
    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer/Contact[text()='Li Li']");
    Console.WriteLine(node.Attributes["gender"].Value);

    (三) 创建

    以创建以下XML文档为例:

    复制代码
    <?xml version="1.0" encoding="UTF-8"?> 
    <Customers> 
      <Customer id="01" name="Lenovo" country="China" city="Beijing"> 
        <Contact title="Support" gender="female">Li Li</Contact> 
      </Customer> 
    </Customers>
    复制代码
    复制代码
    var xmlDoc = new XmlDocument(); 
    //Create the xml declaration first 
    xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
    
    //Create the root node and append into doc 
    var el = xmlDoc.CreateElement("Customers"); 
    xmlDoc.AppendChild(el);
    
    // Customer Lenovo 
    XmlElement elementCustomer = xmlDoc.CreateElement("Customer");
    
    XmlAttribute attrID = xmlDoc.CreateAttribute("id"); 
    attrID.Value = "01"; 
    elementCustomer.Attributes.Append(attrID);
    
    XmlAttribute cityID = xmlDoc.CreateAttribute("city"); 
    cityID.Value = "Beijing"; 
    elementCustomer.Attributes.Append(cityID);
    
    XmlAttribute attrCountry = xmlDoc.CreateAttribute("country"); 
    attrCountry.Value = "China"; 
    elementCustomer.Attributes.Append(attrCountry);
    
    XmlAttribute nameCountry = xmlDoc.CreateAttribute("name"); 
    nameCountry.Value = "Lenovo"; 
    elementCustomer.Attributes.Append(nameCountry);
    
    el.AppendChild(elementCustomer);
    
    // Contact Li Li 
    XmlElement elementContact = xmlDoc.CreateElement("Contact"); 
    elementContact.InnerText = "Li Li";
    
    XmlAttribute attrGender = xmlDoc.CreateAttribute("gender"); 
    attrGender.Value = "female"; 
    elementContact.Attributes.Append(attrGender);
    
    XmlAttribute titleGender = xmlDoc.CreateAttribute("title"); 
    titleGender.Value = "Support"; 
    elementContact.Attributes.Append(titleGender);
    
    elementCustomer.AppendChild(elementContact);
    
    xmlDoc.Save("test.xml"); 
    复制代码

    (四) 更改

    还以下面的XML文档为例

    复制代码
    <?xml version="1.0" encoding="utf-8" ?> 
    <Customers> 
      <Customer id="01" city="Beijing" country="China" name="Lenovo"> 
        <Contact gender="female" title="Support">Li Li</Contact> 
      </Customer> 
      <Customer id="02" city="Amsterdam" country="The Netherlands" name="Shell"> 
        <Contact gender="male" title="Sales Person">Aaron Babbitt</Contact> 
        <Contact gender="female" title="Sales Manager">Daisy Cabell</Contact> 
        <Contact gender="male" title="Sales Person">Gabriel Eads</Contact> 
      </Customer> 
    </Customers>
    复制代码

    1. 将Contact Li Li更改为Evi

    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer/Contact[text()='Li Li']");
    node.InnerText = "Evi";

    2. 将id为02的Customer的city属性更改为Den Haag

    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer[@id='02']");
    node.Attributes["city"].Value = "Den Haag";


    3. 将id为02的Customer的所有下属Contact的title属性更改为Functional Consultant

    复制代码
    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer[@id='02']");
    XmlNodeList nodelist = node.SelectNodes("./Contact");
    foreach (XmlNode n in nodelist)
    {
        n.Attributes["title"].Value = "Functional Consultant";
    }
    复制代码

    (五) 删除节点

    还是以这个XML文档为例

    复制代码
    <?xml version="1.0" encoding="utf-8" ?> 
    <Customers> 
      <Customer id="01" city="Beijing" country="China" name="Lenovo"> 
        <Contact gender="female" title="Support">Li Li</Contact> 
      </Customer> 
      <Customer id="02" city="Amsterdam" country="The Netherlands" name="Shell"> 
        <Contact gender="male" title="Sales Person">Aaron Babbitt</Contact> 
        <Contact gender="female" title="Sales Manager">Daisy Cabell</Contact> 
        <Contact gender="male" title="Sales Person">Gabriel Eads</Contact> 
      </Customer> 
    </Customers>
    复制代码


    1. 删除Contact Li Li节点

    XmlNode node = xmlDoc.SelectSingleNode("/Customers/Customer/Contact[text()='Li Li']");            
    node.ParentNode.RemoveChild(node);

    结果:

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <Customers>
      <Customer id="01" city="Beijing" country="China" name="Lenovo">
      </Customer>
      <Customer id="02" city="Amsterdam" country="The Netherlands" name="Shell">
        <Contact gender="male" title="Sales Person">Aaron Babbitt</Contact>
        <Contact gender="female" title="Sales Manager">Daisy Cabell</Contact>
        <Contact gender="male" title="Sales Person">Gabriel Eads</Contact>
      </Customer>
    </Customers>
    复制代码

    (六) Namespace

    XML文档中如果使用了命名空间该如何进行查询,以下面的文档为例:

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <root xmlns:h="http://www.w3.org/TR/html4/"
     xmlns:f="http://www.w3schools.com/furniture">
      <h:table>
        <h:tr>
          <h:td>Apples</h:td>
          <h:td>Bananas</h:td>
        </h:tr>
      </h:table>
      <f:table>
        <f:name>African Coffee Table</f:name>
        <f:width>80</f:width>
        <f:length>120</f:length>
      </f:table>
    </root>
    复制代码

    查找第一个f:name节点

    var names = new XmlNamespaceManager(xmlDoc.NameTable);
    names.AddNamespace("h", "http://www.w3.org/TR/html4/");
    names.AddNamespace("f", "http://www.w3schools.com/furniture");
    XmlNode node = xmlDoc.SelectSingleNode("/root/f:table/f:name", names);
    Console.WriteLine(node.InnerText);

    总结: XmlDocument类是.Net API中提供的支持W3C XML DOM标准的类。可以用它来创建和查询XML文档。由于XmlDocument要将XML文档的内容全部装载进内存中,所以对于读取内容过大的XML文档,不适合使用XmlDocument类,而可以使用XmlReader来完成读取。

  • 相关阅读:
    BCrypt 加密实现
    Java AES512加密算法
    二、 Mosquitto 使用说明
    一、Mosquitto 介绍&安装
    ConcurrentHashMap源码分析(JDK8版本<转载>)
    详解设计模式六大原则(转载)
    Java 单例模式详解(转)
    一致性哈希算法(consistent hashing)(转载)
    Java并发之AQS详解(转)
    数据库(第一范式,第二范式,第三范式)(转)
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3335287.html
Copyright © 2011-2022 走看看