您的位置:首页 > 编程语言 > C#

C#操作XML文档

2012-12-18 11:03 337 查看
c#读写xml文件

已知有一个XML文件(bookstore.xml)如下:

<?xml version="1.0" encoding="gb2312"?>

<bookstore>

  <book genre="fantasy" ISBN="2-3631-4">

    <title>Oberon's Legacy</title>

    <author>Corets, Eva</author>

    <price>5.95</price>

  </book>

</bookstore>

 

1、往<bookstore>节点中插入一个<book>节点:

   XmlDocument xmlDoc=new XmlDocument();

   xmlDoc.Load("bookstore.xml");

   XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>

   XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点

   xe1.SetAttribute("genre","李赞红");//设置该节点genre属性

   xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性

 

   XmlElement xesub1=xmlDoc.CreateElement("title");

   xesub1.InnerText="CS从入门到精通";//设置文本节点

   xe1.AppendChild(xesub1);//添加到<book>节点中

   XmlElement xesub2=xmlDoc.CreateElement("author");

   xesub2.InnerText="候捷";

   xe1.AppendChild(xesub2);

   XmlElement xesub3=xmlDoc.CreateElement("price");

   xesub3.InnerText="58.3";

   xe1.AppendChild(xesub3);

 

   root.AppendChild(xe1);//添加到<bookstore>节点中

   xmlDoc.Save("bookstore.xml");

//===============================================

结果为:

<?xml version="1.0" encoding="gb2312"?>

<bookstore>

  <book genre="fantasy" ISBN="2-3631-4">

    <title>Oberon's Legacy</title>

    <author>Corets, Eva</author>

    <price>5.95</price>

  </book>

  <book genre="李赞红" ISBN="2-3631-4">

    <title>CS从入门到精通</title>

    <author>候捷</author>

    <price>58.3</price>

  </book>

</bookstore>

 

2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。

    XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点

   foreach(XmlNode xn in nodeList)//遍历所有子节点

   {

    XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型

    if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”

    {

     xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”

 

     XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点

     foreach(XmlNode xn1 in nls)//遍历

     {

      XmlElement xe2=(XmlElement)xn1;//转换类型

      if(xe2.Name=="author")//如果找到

      {

       xe2.InnerText="亚胜";//则修改

       break;//找到退出来就可以了

      }

     }

     break;

    }

   }

 

   xmlDoc.Save("bookstore.xml");//保存。

//==================================================

最后结果为:

<?xml version="1.0" encoding="gb2312"?>

<bookstore>

  <book genre="fantasy" ISBN="2-3631-4">

    <title>Oberon's Legacy</title>

    <author>Corets, Eva</author>

    <price>5.95</price>

  </book>

  <book genre="update李赞红" ISBN="2-3631-4">

    <title>CS从入门到精通</title>

    <author>亚胜</author>

    <price>58.3</price>

  </book>

</bookstore>

 

3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。

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");

//===========================================

最后结果为:

<?xml version="1.0" encoding="gb2312"?>

<bookstore>

  <book ISBN="2-3631-4">

    <title>Oberon's Legacy</title>

    <author>Corets, Eva</author>

    <price>5.95</price>

  </book>

  <book>

  </book>

</bookstore>

 

4、显示所有数据。

   XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

 

   XmlNodeList xnl=xn.ChildNodes;

  

   foreach(XmlNode xnf in xnl)

   {

    XmlElement xe=(XmlElement)xnf;

    Console.WriteLine(xe.GetAttribute("genre"));//显示属性值

    Console.WriteLine(xe.GetAttribute("ISBN"));

 

    XmlNodeList xnf1=xe.ChildNodes;

    foreach(XmlNode xn2 in xnf1)

    {

     Console.WriteLine(xn2.InnerText);//显示子节点点文本

    }

   }

这个XML文档的操作管理帮助类XMLHelper出来,这个XMLHelper类中包括了XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现,有兴趣的朋友,可以进来看看,所有代码都在WebForm和WinForm中调试通过.

这是下面要操作的XML文档:



<?xml version="1.0" encoding="utf-8"?>
<books>

  <book id="1" ISDN="1001001001">

    <name>我的世界我的梦</name>

    <author>姚明</author>

    <date>2008-09-23</date>

  </book>

  <book id="2" ISDN="2002000230032">

    <name>围城</name>

    <author>钱钟书</author>

    <date>2008-09-23</date>

  </book>

  <book id="3" />
</books>
 

以下是XMLHelper文档操作帮助类代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Xml;

namespace WebApplication2

{

    /// <summary>

    /// XMLHelper XML文档操作管理器

    /// </summary>
    public class XMLHelper

    {

        public XMLHelper()

        {

            //

            // TODO: 在此处添加构造函数逻辑

            //
        }

        #region XML文档节点查询和读取

        /// <summary>

        /// 选择匹配XPath表达式的第一个节点XmlNode.

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>

        /// <returns>返回XmlNode</returns>
        public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath)

        {

            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                return xmlNode;

            }

            catch (Exception ex)

            {

                return null;

                //throw ex; //这里可以定义你自己的异常处理
            }

        }

        /// <summary>

        /// 选择匹配XPath表达式的节点列表XmlNodeList.

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名")</param>

        /// <returns>返回XmlNodeList</returns>
        public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath)

        {

            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);

                return xmlNodeList;

            }

            catch (Exception ex)

            {

                return null;

                //throw ex; //这里可以定义你自己的异常处理
            }

        }

        /// <summary>

        /// 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute.

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>

        /// <returns>返回xmlAttributeName</returns>
        public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName)

        {

            string content = string.Empty;

            XmlDocument xmlDoc = new XmlDocument();

            XmlAttribute xmlAttribute = null;

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                if (xmlNode != null)

                {

                    if (xmlNode.Attributes.Count > 0)

                    {

                        xmlAttribute = xmlNode.Attributes[xmlAttributeName];

                    }

                }

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return xmlAttribute;

        }

        #endregion

        #region XML文档创建和节点或属性的添加、修改

        /// <summary>

        /// 创建一个XML文档

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="rootNodeName">XML文档根节点名称(须指定一个根节点名称)</param>

        /// <param name="version">XML文档版本号(必须为:"1.0")</param>

        /// <param name="encoding">XML文档编码方式</param>

        /// <param name="standalone">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone)

        {

            bool isSuccess = false;

            try

            {

                XmlDocument xmlDoc = new XmlDocument();

                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);

                XmlNode root = xmlDoc.CreateElement(rootNodeName);

                xmlDoc.AppendChild(xmlDeclaration);

                xmlDoc.AppendChild(root);

                xmlDoc.Save(xmlFileName);

                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        /// <summary>

        /// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>

        /// <param name="innerText">节点文本值</param>

        /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>

        /// <param name="value">属性值</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string xmlAttributeName, string value)

        {

            bool isSuccess = false;

            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                if (xmlNode != null)

                {

                    //存不存在此节点都创建
                    XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);

                    subElement.InnerXml = innerText;

                    //如果属性和值参数都不为空则在此新节点上新增属性
                    if (!string.IsNullOrEmpty(xmlAttributeName) && !string.IsNullOrEmpty(value))

                    {

                        XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);

                        xmlAttribute.Value = value;

                        subElement.Attributes.Append(xmlAttribute);

                    }

                    xmlNode.AppendChild(subElement);

                }

                xmlDoc.Save(xmlFileName); //保存到XML文档
                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        /// <summary>

        /// 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建)

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <param name="xmlNodeName">要匹配xmlNodeName的节点名称</param>

        /// <param name="innerText">节点文本值</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText)

        {

            bool isSuccess = false;

            bool isExistsNode = false;//标识节点是否存在
            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                if (xmlNode != null)

                {

                    //遍历xpath节点下的所有子节点
                    foreach (XmlNode node in xmlNode.ChildNodes)

                    {

                        if (node.Name.ToLower() == xmlNodeName.ToLower())

                        {

                            //存在此节点则更新
                            node.InnerXml = innerText;

                            isExistsNode = true;

                            break;

                        }

                    }

                    if (!isExistsNode)

                    {

                        //不存在此节点则创建
                        XmlElement subElement = xmlDoc.CreateElement(xmlNodeName);

                        subElement.InnerXml = innerText;

                        xmlNode.AppendChild(subElement);

                    }

                }

                xmlDoc.Save(xmlFileName); //保存到XML文档
                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        /// <summary>

        /// 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建)

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <param name="xmlAttributeName">要匹配xmlAttributeName的属性名称</param>

        /// <param name="value">属性值</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value)

        {

            bool isSuccess = false;

            bool isExistsAttribute = false;//标识属性是否存在
            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                if (xmlNode != null)

                {

                    //遍历xpath节点中的所有属性
                    foreach (XmlAttribute attribute in xmlNode.Attributes)

                    {

                        if (attribute.Name.ToLower() == xmlAttributeName.ToLower())

                        {

                            //节点中存在此属性则更新
                            attribute.Value = value;

                            isExistsAttribute = true;

                            break;

                        }

                    }

                    if (!isExistsAttribute)

                    {

                        //节点中不存在此属性则创建
                        XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName);

                        xmlAttribute.Value = value;

                        xmlNode.Attributes.Append(xmlAttribute);

                    }

                }

                xmlDoc.Save(xmlFileName); //保存到XML文档
                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        #endregion

        #region XML文档节点或属性的删除

        /// <summary>

        /// 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除)

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath)

        {

            bool isSuccess = false;

            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                if (xmlNode != null)

                {

                    //删除节点
                    xmlNode.ParentNode.RemoveChild(xmlNode);

                }

                xmlDoc.Save(xmlFileName); //保存到XML文档
                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        /// <summary>

        /// 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <param name="xmlAttributeName">要删除的xmlAttributeName的属性名称</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName)

        {

            bool isSuccess = false;

            bool isExistsAttribute = false;

            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                XmlAttribute xmlAttribute = null;

                if (xmlNode != null)

                {

                    //遍历xpath节点中的所有属性
                    foreach (XmlAttribute attribute in xmlNode.Attributes)

                    {

                        if (attribute.Name.ToLower() == xmlAttributeName.ToLower())

                        {

                            //节点中存在此属性
                            xmlAttribute = attribute;

                            isExistsAttribute = true;

                            break;

                        }

                    }

                    if (isExistsAttribute)

                    {

                        //删除节点中的属性
                        xmlNode.Attributes.Remove(xmlAttribute);

                    }

                }

                xmlDoc.Save(xmlFileName); //保存到XML文档
                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        /// <summary>

        /// 删除匹配XPath表达式的第一个节点中的所有属性

        /// </summary>

        /// <param name="xmlFileName">XML文档完全文件名(包含物理路径)</param>

        /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>

        /// <returns>成功返回true,失败返回false</returns>
        public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath)

        {

            bool isSuccess = false;

            XmlDocument xmlDoc = new XmlDocument();

            try

            {

                xmlDoc.Load(xmlFileName); //加载XML文档
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);

                if (xmlNode != null)

                {

                    //遍历xpath节点中的所有属性
                    xmlNode.Attributes.RemoveAll();

                }

                xmlDoc.Save(xmlFileName); //保存到XML文档
                isSuccess = true;

            }

            catch (Exception ex)

            {

                throw ex; //这里可以定义你自己的异常处理
            }

            return isSuccess;

        }

        #endregion

    }

}

 

1.创建XML文档:

     //这是XML文档根节点名
            string rootNodeName = "books";

            

            //这是XML文档物理文件名(包含物理路径)
            string xmlFileName = Application.StartupPath + @"\book.xml";

            XMLHelper.CreateXmlDocument(xmlFileName, rootNodeName, "1.0", "utf-8", null);

            MessageBox.Show("XML文档创建成功:" + xmlFileName);  

2.向XML文档中添加一个新节点:

            string xmlFileName = Application.StartupPath + @"\book.xml";

            string xpath = "/books";  //这是新节点的父节点路径
            string nodename = "book"; //这是新节点名称,在父节点下新增
            string nodetext = "这是新节点中的文本值";

            bool isSuccess = XMLHelper.CreateOrUpdateXmlNodeByXPath(xmlFileName, xpath, nodename, nodetext);

            MessageBox.Show("XML节点添加或更新成功:" + isSuccess.ToString());

 
 

3.向XML文档中的子节点中新增或修改(如果存在则修改)一个子节点,比如name,author,date节点等:

 

            string xmlFileName = Application.StartupPath + @"\book.xml";

            string xpath = "/books/book";  //这是新子节点的父节点路径
            string nodename = "name"; //这是新子节点名称,在父节点下新增
            string nodetext = "我的世界我的梦";

            bool isSuccess = XMLHelper.CreateOrUpdateXmlNodeByXPath(xmlFileName, xpath, nodename, nodetext);

            MessageBox.Show("XML节点添加或更新成功:" + isSuccess.ToString());

4. 向XML文档中的子节点中新增或修改(如果存在则修改)一个子节点属性,比如id,ISDN属性等:

            string xmlFileName = Application.StartupPath + @"\book.xml";

            string xpath = "/books/book"; //要新增属性的节点
            string attributeName = "id"; //新属性名称,ISDN号也是这么新增的
            string attributeValue = "1"; //新属性值

            bool isSuccess = XMLHelper.CreateOrUpdateXmlAttributeByXPath(xmlFileName, xpath, attributeName, attributeValue);

            MessageBox.Show("XML属性添加或更新成功:" + isSuccess.ToString());

5. 删除XML文档中的子节点:

            string xmlFileName = Application.StartupPath + @"\book.xml";

            string xpath = "/books/book[@id='1']"; //要删除的id为1的book子节点

            bool isSuccess = XMLHelper.DeleteXmlNodeByXPath(xmlFileName, xpath);

            MessageBox.Show("XML节点删除成功:" + isSuccess.ToString());  

6. 删除XML文档中子节点的属性:

            string xmlFileName = Application.StartupPath + @"\book.xml";

            //删除id为2的book子节点中的ISDN属性
            string xpath = "/books/book[@id='2']";

            string attributeName = "ISDN";

            bool isSuccess = XMLHelper.DeleteXmlAttributeByXPath(xmlFileName, xpath,attributeName);

            MessageBox.Show("XML属性删除成功:" + isSuccess.ToString());  

7.读取XML文档中的所有子节点:
            string xmlFileName = Application.StartupPath + @"\book.xml";
//要读的id为1的book子节点
            string xpath = "/books/book[@id='1']";

            XmlNodeList nodeList = XMLHelper.GetXmlNodeListByXpath(xmlFileName, xpath);

            string strAllNode = "";

            //遍历节点中所有的子节点
            foreach (XmlNode node in nodeList)

            {

                strAllNode += "\n name:" + node.Name + " InnerText:" + node.InnerText;

            }

            MessageBox.Show("XML节点中所有子节点有:" + strAllNode); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: