VS 2005 C#操作 XML 收藏

XML大家一定都很熟悉了,根据前一阵做项目总结出的经验,写一篇关于在VS 2005下操作XML的文章,语言使用C#
准备工作
要添加的命名空间为 System.Xml  System.Xml.XPath
第一章 在内存里建立Xml对象
有时候我们可能需要根据数据库里的数据值生成Xml文件,那么,我们就要在内存里先建立Xml对象,之后再将Xml对象以字符串或文件的形式输出。首先来看看怎么用.Net下的类所提供的方法来生成Xml的各部分
假设一软件商买如下游戏:文明3,帝国时代
那么以下代码将生成如下的Xml文档 <script type="text/JavaScript"></script> <script src="http://a.alimama.cn/inf.js" type="text/javascript"></script>

          <?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>


 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,完整的代码如下:

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”有关的节点时,可以这样:
 
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这个属性
还有一个方法可以设置节点的属生及节点的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例,演示如何使用 C# 操作 XML 文件: ```csharp using System; using System.Xml; public class XmlHandler { private XmlDocument xmlDoc; public XmlHandler(string xmlFilePath) { xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); } public XmlNodeList GetElementsByTagName(string tagName) { return xmlDoc.GetElementsByTagName(tagName); } public void AddChildNode(string parentNodeName, string childNodeName, string innerText) { XmlNode parentNode = xmlDoc.SelectSingleNode(parentNodeName); XmlElement childNode = xmlDoc.CreateElement(childNodeName); childNode.InnerText = innerText; parentNode.AppendChild(childNode); xmlDoc.Save(xmlDoc.BaseURI); } public void UpdateNodeValue(string nodeName, string newValue) { XmlNode node = xmlDoc.SelectSingleNode(nodeName); node.InnerText = newValue; xmlDoc.Save(xmlDoc.BaseURI); } public void DeleteNode(string nodeName) { XmlNode node = xmlDoc.SelectSingleNode(nodeName); node.ParentNode.RemoveChild(node); xmlDoc.Save(xmlDoc.BaseURI); } } ``` 在上面的代码示例中,我们创建了一个 `XmlHandler` 类,它可以加载 XML 文件,并提供了一些操作 XML 文件的方法: - `GetElementsByTagName(tagName)` 方法可以通过标签名获取节点列表。 - `AddChildNode(parentNodeName, childNodeName, innerText)` 方法可以在指定的父节点下添加一个子节点,并设置它的文本内容。 - `UpdateNodeValue(nodeName, newValue)` 方法可以更新指定节点的文本内容。 - `DeleteNode(nodeName)` 方法可以除指定的节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值