zoukankan      html  css  js  c++  java
  • c#操作XML文件的通用方法

     转载地址:http://www.studyofnet.com/news/36.html

    原址没找到

                    sing System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml;
    using System.Collections;
    using System.IO;
    
    
    public class XMLHealper
    {
       构造函数#region 构造函数
        public XMLHealper()
       {
            FilePath = XMLPath.GetXMLPath();
            OpenXML();
       }
       #endregion
    
       对象定义#region 对象定义
    
        private XmlDocument xmlDoc = new XmlDocument();
       XmlNode xmlnode;
       XmlElement xmlelem;
    
       #endregion
    
       属性定义#region 属性定义
    
        private string errorMess;
        public string ErrorMess
        {
            get { return errorMess; }
            set { errorMess = value; }
        }
    
        private string filePath;
        public string FilePath
        {
            set { filePath = value; }
            get { return filePath; }
        }
    
        #endregion
    
        加载XML文件,创建操作对象#region 加载XML文件,创建操作对象
         /**//// <summary>
        /// 加载XML文件,创建操作对象
        /// </summary>
        public void OpenXML()
        {
            try
            {
                if (!string.IsNullOrEmpty(FilePath))
                {
                    xmlDoc.Load(filePath);
                }
                else
                {
                    FilePath = string.Format(@"D:\XMLExample.xml"); //默认路径
                    xmlDoc.Load(filePath);
                }
            }
            catch (Exception ex)
            {
                ErrorMess = ex;
            }
        }
        #endregion
    
        创建Xml 文档#region 创建Xml 文档
        /**//// <summary>
        /// 创建一个带有根节点的Xml 文件
        /// </summary>
        /// <param name="FileName">Xml 文件名称</param>
        /// <param name="rootName">根节点名称</param>
        /// <param name="Encode">编码方式:gb2312,UTF-8 等常见的</param>
        /// <param name="DirPath">保存的目录路径</param>
        /// <returns></returns>
        public bool CreatexmlDocument(string FileName, string rootName, string Encode)
        {
            try
            {
                XmlDeclaration xmldecl;
                xmldecl = xmlDoc.CreateXmlDeclaration("1.0", Encode, null);
                xmlDoc.AppendChild(xmldecl);
                xmlelem = xmlDoc.CreateElement("", rootName, "");
                xmlDoc.AppendChild(xmlelem);
                xmlDoc.Save(FileName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        #endregion
    
        得到数据表#region 得到数据表
        /**//// <summary>
        /// 得到数据表
        /// </summary>
        /// <returns></returns>
        public DataView GetData()
        {
            xmlDoc = new XmlDocument();
            DataSet ds = new DataSet();
            StringReader read = new StringReader(xmlDoc.SelectSingleNode(FilePath).OuterXml);
            ds.ReadXml(read);
            return ds.Tables[0].DefaultView;
        }
        #endregion
    
        读取指定节点的指定属性值#region 读取指定节点的指定属性值
        /**//// <summary>
        /// 功能:
        /// 读取指定节点的指定属性值
        /// </summary>
        /// <param name="strNode">节点名称(相对路径://+节点名称)</param>
        /// <param name="strAttribute">此节点的属性</param>
        /// <returns></returns>
        public string GetXmlNodeValue(string strNode, string strAttribute)
        {
            string strReturn = "";
            try
            {
                //根据指定路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
                //获取节点的属性,并循环取出需要的属性值
                XmlAttributeCollection xmlAttr = xmlNode.Attributes;
    
                for (int i = 0; i < xmlAttr.Count; i++)
                {
                    if (xmlAttr.Item(i).Name == strAttribute)
                    {
                        strReturn = xmlAttr.Item(i).Value;
                    }
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }
        #endregion
    
        读取指定节点的值#region 读取指定节点的值
        /**//// <summary>
        /// 读取指定节点的值
        /// </summary>
        /// <param name="strNode">节点名称</param>
        /// <returns></returns>
        public string GetXmlNodeValue(string strNode)
        {
            string strReturn = String.Empty;
            try
            {
                //根据路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
                strReturn = xmlNode.InnerText;
            }
            catch (XmlException xmle)
            {
                System.Console.WriteLine(xmle.Message);
            }
            return strReturn;
        }
        #endregion
    
        获取XML文件的根元素#region 获取XML文件的根元素
        /**//// <summary>
        /// 获取XML文件的根元素
        /// </summary>
        public XmlNode GetXmlRoot()
        {
            return xmlDoc.DocumentElement;
        }
        #endregion
    
        获取XML节点值#region 获取XML节点值
        /**//// <summary>
        /// 获取XML节点值
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public string GetNodeValue(string nodeName)
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(FilePath);
    
            XmlNodeList xnl = xmlDoc.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                XmlElement xe = (XmlElement)xnf;
                XmlNodeList xnf1 = xe.ChildNodes;
                foreach (XmlNode xn2 in xnf1)
                {
                    if (xn2.InnerText == nodeName)
                    {
                        XmlElement xe2 = (XmlElement)xn2;
                        return xe2.GetAttribute("value");
                    }
                }
            }
            return null;
        }
        #endregion
    
    
        设置节点值#region 设置节点值
        /**//// <summary>
        /// 功能:
        /// 设置节点值
        /// </summary>
        /// <param name="strNode">节点的名称</param>
        /// <param name="newValue">节点值</param>
        public void SetXmlNodeValue(string xmlNodePath, string xmlNodeValue)
        {
            try
            {
                //根据指定路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath);
                //设置节点值
                xmlNode.InnerText = xmlNodeValue;
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
        #endregion
    
        添加父节点#region 添加父节点
    
        /**//// <summary>
        /// 在根节点下添加父节点
        /// </summary>
        public void AddParentNode(string parentNode)
        {
            XmlNode root = GetXmlRoot();
            XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);
    
            root.AppendChild(parentXmlNode);
        }
        #endregion
    
        向一个已经存在的父节点中插入一个子节点#region 向一个已经存在的父节点中插入一个子节点
        /**//// <summary>
        /// 向一个已经存在的父节点中插入一个子节点
        /// </summary>
        public void AddChildNode(string parentNodePath, string childNodePath)
        {
            XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
            XmlNode childXmlNode = xmlDoc.CreateElement(childNodePath);
    
            parentXmlNode.AppendChild(childXmlNode);
        }
        #endregion
    
        向一个节点添加属性#region 向一个节点添加属性
        /**//// <summary>
        /// 向一个节点添加属性
        /// </summary>
        public void AddAttribute(string NodePath, string NodeAttribute)
        {
            XmlAttribute nodeAttribute = xmlDoc.CreateAttribute(NodeAttribute);
            XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);
            nodePath.Attributes.Append(nodeAttribute);
        }
        #endregion
    
        插入一个节点和它的若干子节点#region 插入一个节点和它的若干子节点
        /**//// <summary>
        /// 插入一个节点和它的若干子节点
        /// </summary>
        /// <param name="NewNodeName">插入的节点名称</param>
        /// <param name="HasAttributes">此节点是否具有属性,True 为有,False 为无</param>
        /// <param name="fatherNode">此插入节点的父节点</param>
        /// <param name="htAtt">此节点的属性,Key 为属性名,Value 为属性值</param>
        /// <param name="htSubNode"> 子节点的属性, Key 为Name,Value 为InnerText</param>
        /// <returns>返回真为更新成功,否则失败</returns>
        public bool InsertNode(string NewNodeName, bool HasAttributes, string fatherNode, Hashtable htAtt, Hashtable htSubNode)
        {
            try
            {
                xmlDoc.Load(FilePath);
                XmlNode root = xmlDoc.SelectSingleNode(fatherNode);
                xmlelem = xmlDoc.CreateElement(NewNodeName);
                if (htAtt != null && HasAttributes)//若此节点有属性,则先添加属性
                {
                    SetAttributes(xmlelem, htAtt);
                    AddNodes(xmlelem.Name, xmlDoc, xmlelem, htSubNode);//添加完此节点属性后,再添加它的子节点和它们的InnerText
                }
                else
                {
                    AddNodes(xmlelem.Name, xmlDoc, xmlelem, htSubNode);//若此节点无属性,那么直接添加它的子节点
                }
                root.AppendChild(xmlelem);
                xmlDoc.Save(FilePath);
                return true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        #endregion
    
        设置节点属性#region 设置节点属性
        /**//// <summary>
        /// 设置节点属性
        /// </summary>
        /// <param name="xe">节点所处的Element</param>
        /// <param name="htAttribute">节点属性,Key 代表属性名称,Value 代表属性值</param>
        public void SetAttributes(XmlElement xe, Hashtable htAttribute)
        {
            foreach (DictionaryEntry de in htAttribute)
            {
                xe.SetAttribute(de.Key.ToString(), de.Value.ToString());
            }
        }
        #endregion
    
        增加子节点到根节点下#region 增加子节点到根节点下
        /**//// <summary>
        /// 增加子节点到根节点下
        /// </summary>
        /// <param name="rootNode">上级节点名称</param>
        /// <param name="xmlDoc">Xml 文档</param>
        /// <param name="rootXe">父根节点所属的Element</param>
        /// <param name="SubNodes">子节点属性,Key 为Name 值,Value 为InnerText 值</param>
        public void AddNodes(string rootNode, XmlDocument xmlDoc, XmlElement rootXe, Hashtable SubNodes)
        {
            foreach (DictionaryEntry de in SubNodes)
            {
                xmlnode = xmlDoc.SelectSingleNode(rootNode);
                XmlElement subNode = xmlDoc.CreateElement(de.Key.ToString());
                subNode.InnerText = de.Value.ToString();
                rootXe.AppendChild(subNode);
            }
        }
        #endregion
    
        //更新
    
        设置节点的属性值#region 设置节点的属性值
        /**//// <summary>
        /// 功能:
        /// 设置节点的属性值
        /// </summary>
        /// <param name="xmlNodePath">节点名称</param>
        /// <param name="xmlNodeAttribute">属性名称</param>
        /// <param name="xmlNodeAttributeValue">属性值</param>
        public void SetXmlNodeValue(string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)
        {
            try
            {
                //根据指定路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath);
    
                //获取节点的属性,并循环取出需要的属性值
                XmlAttributeCollection xmlAttr = xmlNode.Attributes;
                for (int i = 0; i < xmlAttr.Count; i++)
                {
                    if (xmlAttr.Item(i).Name == xmlNodeAttribute)
                    {
                        xmlAttr.Item(i).Value = xmlNodeAttributeValue;
                        break;
                    }
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
    
        #endregion
    
        更新节点#region 更新节点
        /**//// <summary>
        /// 更新节点
        /// </summary>
        /// <param name="fatherNode">需要更新节点的上级节点</param>
        /// <param name="htAtt">需要更新的属性表,Key 代表需要更新的属性,Value 代表更新后的值</param>
        /// <param name="htSubNode">需要更新的子节点的属性表,Key 代表需要更新的子节点名字Name,Value 代表更新后的值InnerText</param>
        /// <returns>返回真为更新成功,否则失败</returns>
        public bool UpdateNode(string fatherNode, Hashtable htAtt, Hashtable htSubNode)
        {
            try
            {
                xmlDoc = new XmlDocument();
                xmlDoc.Load(FilePath);
                XmlNodeList root = xmlDoc.SelectSingleNode(fatherNode).ChildNodes;
                UpdateNodes(root, htAtt, htSubNode);
                xmlDoc.Save(FilePath);
                return true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        #endregion
    
        更新节点属性和子节点InnerText 值#region 更新节点属性和子节点InnerText 值
        /**//// <summary>
        /// 更新节点属性和子节点InnerText 值
        /// </summary>
        /// <param name="root">根节点名字</param>
        /// <param name="htAtt">需要更改的属性名称和值</param>
        /// <param name="htSubNode">需要更改InnerText 的子节点名字和值</param>
        public void UpdateNodes(XmlNodeList root, Hashtable htAtt, Hashtable htSubNode)
        {
            foreach (XmlNode xn in root)
            {
                xmlelem = (XmlElement)xn;
                if (xmlelem.HasAttributes)//如果节点如属性,则先更改它的属性
                {
                    foreach (DictionaryEntry de in htAtt)//遍历属性哈希表
                    {
                        if (xmlelem.HasAttribute(de.Key.ToString()))//如果节点有需要更改的属性
                        {
                            xmlelem.SetAttribute(de.Key.ToString(), de.Value.ToString());//则把哈希表中相应的值Value 赋给此属性Key
                        }
                    }
                }
                if (xmlelem.HasChildNodes)//如果有子节点,则修改其子节点的InnerText
                {
                    XmlNodeList xnl = xmlelem.ChildNodes;
                    foreach (XmlNode xn1 in xnl)
                    {
                        XmlElement xe = (XmlElement)xn1;
                        foreach (DictionaryEntry de in htSubNode)
                        {
                            if (xe.Name == de.Key.ToString())//htSubNode 中的key 存储了需要更改的节点名称,
                            {
                                xe.InnerText = de.Value.ToString();//htSubNode中的Value存储了Key 节点更新后的数据
                            }
                        }
                    }
                }
            }
        }
        #endregion
    
    
        删除一个节点的属性#region 删除一个节点的属性
        /**//// <summary>
        /// 删除一个节点的属性
        /// </summary>
        public void DeleteAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
        {
            XmlNodeList nodePath = xmlDoc.SelectSingleNode(NodePath).ChildNodes;
    
            foreach (XmlNode xn in nodePath)
            {
                XmlElement xe = (XmlElement)xn;
    
                if (xe.GetAttribute(NodeAttribute) == NodeAttributeValue)
                {
                    xe.RemoveAttribute(NodeAttribute);//删除属性
                }
            }
        }
    
        #endregion
    
        删除一个节点#region 删除一个节点
        /**//// <summary>
        /// 删除一个节点
        /// </summary>
        public void DeleteXmlNode(string tempXmlNode)
        {
            XmlNode xmlNodePath = xmlDoc.SelectSingleNode(tempXmlNode);
            xmlNodePath.ParentNode.RemoveChild(xmlNodePath);
        }
    
        #endregion
    
        删除指定节点下的子节点#region 删除指定节点下的子节点
        /**//// <summary>
        /// 删除指定节点下的子节点
        /// </summary>
        /// <param name="fatherNode">制定节点</param>
        /// <returns>返回真为更新成功,否则失败</returns>
        public bool DeleteNodes(string fatherNode)
        {
            try
            {
                xmlDoc = new XmlDocument();
                xmlDoc.Load(FilePath);
                xmlnode = xmlDoc.SelectSingleNode(fatherNode);
                xmlnode.RemoveAll();
                xmlDoc.Save(FilePath);
                return true;
            }
            catch (XmlException xe)
            {
                throw new XmlException(xe.Message);
            }
        }
        #endregion
    
    
        私有函数#region 私有函数
    
        private string functionReturn(XmlNodeList xmlList, int i, string nodeName)
        {
            string node = xmlList[i].ToString();
            string rusultNode = "";
            for (int j = 0; j < i; j++)
            {
                if (node == nodeName)
                {
                    rusultNode = node.ToString();
                }
                else
                {
                    if (xmlList[j].HasChildNodes)
                    {
                        functionReturn(xmlList, j, nodeName);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return rusultNode;
    
    
        }
    
        #endregion
    
        保存XML文件#region 保存XML文件
        /**//// <summary>
        /// 功能: 
        /// 保存XML文件
        /// 
        /// </summary>
        public void SavexmlDocument()
        {
            try
            {
                xmlDoc.Save(FilePath);
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
        /**//// <summary>
        /// 功能: 保存XML文件
        /// </summary>
        /// <param name="tempXMLFilePath"></param>
        public void SavexmlDocument(string tempXMLFilePath)
        {
            try
            {
                xmlDoc.Save(tempXMLFilePath);
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
        #endregion
    }
    
                

    转载地址:http://www.cnblogs.com/zhouyunbaosujina/archive/2013/09/23/3335287.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来完成读取。

  • 相关阅读:
    Vagrant命令
    您必须知道的 Git 分支开发规范
    phpstorm的提速设置
    Flex 布局教程:语法篇
    Bootstrap 3 & 4 的多级下拉菜单示例
    申请快递查询100接口
    vue.js组件与组件之间的通信
    MAMP环境下 cookie无法正常使用
    Git 常用命令
    Linux学习笔记之yum安装mysql5.7
  • 原文地址:https://www.cnblogs.com/zhouxiuquan/p/3663941.html
Copyright © 2011-2022 走看看