c#操作XML

要添加的命名空间为 System.Xml

第一章 在内存里建立Xml对象

有时候我们可能需要根据数据库里的数据值生成Xml文件,那么,我们就要在内存里先建立Xml对象,之后再将Xml对象以字符串或文件的形式输出。首先来看看怎么用.Net下的类所提供的方法来生成Xml的各部分

假设一软件商买如下游戏:文明3,帝国时代

那么以下代码将生成如下的Xml文档

       <?xml version="1.0" encoding="utf-8" ?>
          <v:Games xmlns:v="www-shop-game">
              <v:Game name="文明3">
                  <Price>100</Price>
              </Game>
              <v:Game name="帝国时代">
                  <Price>200</Price>
              </Game>
          </Games>

view plaincopy to clipboardprint?
XmlDocument xml = new XmlDocument();                        //建立XmlDomcument对象  
                  
XmlDeclaration Declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);    //Xml Declaration(Xml声明)  
 
XmlNode RootNode = xml.CreateNode(XmlNodeType.Element,"v","Games","www-microsoft-game");  
 
xml.AppendChild(RootNode);  
 
XmlNode node1 = xml.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");  
 
RootNode.AppendChild(node1);  
 
node1.Attributes.Append(xml.CreateAttribute("name")).InnerText = "文明3";  
node1.AppendChild(xml.CreateNode(XmlNodeType.Element,"Price",null)).InnerText = "100";  
 
XmlNode node2 = xml.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");  
 
RootNode.AppendChild(node2);  
 
node2.Attributes.Append(xml.CreateAttribute("name")).InnerText = "帝国时代";  
node2.AppendChild(xml.CreateNode(XmlNodeType.Element, "Price", null)).InnerText = "300";  
 
xml.InsertBefore(Declaration, xml.DocumentElement); 
        XmlDocument xml = new XmlDocument();                        //建立XmlDomcument对象
                       
        XmlDeclaration Declaration = xml.CreateXmlDeclaration("1.0", "utf-8", null);    //Xml Declaration(Xml声明)

        XmlNode RootNode = xml.CreateNode(XmlNodeType.Element,"v","Games","www-microsoft-game");

        xml.AppendChild(RootNode);

        XmlNode node1 = xml.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");

        RootNode.AppendChild(node1);

        node1.Attributes.Append(xml.CreateAttribute("name")).InnerText = "文明3";
        node1.AppendChild(xml.CreateNode(XmlNodeType.Element,"Price",null)).InnerText = "100";

        XmlNode node2 = xml.CreateNode(XmlNodeType.Element, "v", "Game", "www-microsoft-game");

        RootNode.AppendChild(node2);

        node2.Attributes.Append(xml.CreateAttribute("name")).InnerText = "帝国时代";
        node2.AppendChild(xml.CreateNode(XmlNodeType.Element, "Price", null)).InnerText = "300";
 
        xml.InsertBefore(Declaration, xml.DocumentElement);

以上虽是一个很简单的Xml文档,但万变不离其中,只要掌握了方法,再复杂的Xml串也可以拼出来。

第二章 读取Xml文档的值 

如果我们有现成的Xml文档,那么,我们怎么取到我们感兴趣的值呢?
 
还是以上边的文档为例,我们看到根节点下边有两个节点,它们的名字都是Game,那么用节点的名称,我们是无法分辨出哪一个节点是“文明3”,哪一个节点是“帝国时代”的!!!那么,当我想知道“文明3”的价格时,就应该使用Xml文档对象下的SelectSingleNode方法。这个方法有两个重载的版本,一个是当Xml文档没有命名空间时使用的,一个参数SelectSingleNode(string xpath)直接传入xpath即可,另一个是当Xml文档有命名空间时使用的,两个参数:SelelctSingleNode(string xpath,XmlNamespaceManager nsmgr),第一个参数还是xpath,第二个参数是XmlNamespaceManager对象,由于我们的Xml是有命名空间的,所以我们要使用第二种方法!首先我们要建立一个XmlNamespaceManager对象,建立的方法为

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new XmlDocument().NameTable);

建立完对象后,我们就要开始为nsmgr增加我们的命名空间了(注意,这里加的命名空间要与Xml实际的相对应,但前辍名可不同),增加的方法如下:
 
nsmgr.AddNamespace("v","www-shop-game")

好了,准备工作完成了,现在可以使用SelectSingleNode方法了
 
string price = xml.SelectSingleNode("v:Games/v:Game[@name='文明3']/Price",nsmgr).InnerText

请注意,如果Xml文档里没有明确指出当前节点的命名空间,那么当前节点的命名空间继承其父节点的命名空间

假设我们刚才生成的Xml文档已存在D:\Xml目录下了,文件的名字就叫sellgame.xml,完整的代码如下:

view plaincopy to clipboardprint?
XmlDocument xml = new XmlDocument();  
xml.Load("D:\\Xml\\sellgame.xml")  
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new XmlDocument().NameTable); //建立Xml命名空间管理器对象  
nsmgr.AddNamespace("v","www-shop-game");                            //增加命名空间  
string price = xml.SelectSingleNode("v:Games/v:Game[@name='文明3']/Price",nsmgr).InnerText;//取得相应的节点值 
XmlDocument xml = new XmlDocument();
xml.Load("D:\\Xml\\sellgame.xml")
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new XmlDocument().NameTable); //建立Xml命名空间管理器对象
nsmgr.AddNamespace("v","www-shop-game");                            //增加命名空间
string price = xml.SelectSingleNode("v:Games/v:Game[@name='文明3']/Price",nsmgr).InnerText;//取得相应的节点值

现在,如果这家软件经销商加增加一种新的产品:文明3的典藏版!那么我们的Xml文档就要进行相应的扩充了:

           <?xml version="1.0" encoding="utf-8" ?>
           <v:Games xmlns:v="www-shop-game">
               <v:Game name="文明3" type="standard">
                   <Price>100</Price>
               </Game>
               <v:Game name="文明3" type="classic">
                   <Price>500</Price>
               </Game>
               <v:Game name="帝国时代">
                   <Price>200</Price>
               </Game>
           </Games>

这时当我们想要取到全部与“文明3”有关的节点时,该如何进行呢?我们可以使用SelectNodes方法

该方法大体上与SelectSingleNode方法一样,也是两次重载,但此方法返回一个XmlNodeList对象,即一个XmlNode数组

当我们想要取到全部与“文明3”有关的节点时,可以这样:

view plaincopy to clipboardprint?
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new XmlDocument().NameTable);    //建立Xml命名空间管理器对象  
 
nsmgr.AddNamespace("v","www-shop-game");                        //增加命名空间  
 
XmlNodeList nodeList = xml.SelectNodes("v:Games/v:Game[@name='文明3']/Price",nsmgr);    //取得相应的节点数组     
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new XmlDocument().NameTable);    //建立Xml命名空间管理器对象

nsmgr.AddNamespace("v","www-shop-game");                        //增加命名空间

XmlNodeList nodeList = xml.SelectNodes("v:Games/v:Game[@name='文明3']/Price",nsmgr);    //取得相应的节点数组   
 

当我们要取节点的属性值及节点的值时,都可以使用InnerText这个属性


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gisfarmer/archive/2009/02/17/3901581.aspx

posted @   94cool  阅读(272)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示