带有命名空间的xml 操作

   这个问题花费了我两天的时间来弄    一般接触xml都是一些简单的操作了    当初遇到问题的时候还是从

   需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。

   这个错误开始了  然后从网上找资料    一点一点积累   用了两天的时间终于将它搞定

 

   前面放网上的资料    因为简单易懂   给需要用到的人一点提示  

  

 

   

 

     在内存里建立Xml对象

         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文档已存在D:/Xml目录下了,文件的名字就叫sellgame.xml,完整的代码如下代码将生成如下的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> 

 

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

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”有关的节点时,可以这样:     

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这个属性。。。。。

 

 

上面的代码也没经过测试    不过用来参考的话就足够了   

 

 

 

   转载自:http://www.glimmer.cn/blog/post/122.html

 

 

 

  接下来就是我自己的代码了  同样给有我这样需求的人一点提示

 

 

  xml文件格式如下:

  <responsedata>
  <autn:numhits xmlns:autn="
http://schemas.autonomy.com/aci/">20</autn:numhits>
  <autn:hit xmlns:autn="
http://schemas.autonomy.com/aci/">
    <autn:reference>v:/20091015/XxjidoC000072_20091015_BJTFN1.xml</autn:reference>
    <autn:id>630352</autn:id>
    <autn:section>0</autn:section>
    <autn:weight>96.00</autn:weight>
    <autn:cluster>1</autn:cluster>
    <autn:clustertitle>工伤 事故, 上半年, 新加坡</autn:clustertitle>
    <autn:database>XINHUA</autn:database>
    <autn:title>(国际社会)新加坡今年上半年发生5000多起工伤事故</autn:title>
  </autn:hit>
  <autn:hit xmlns:autn="
http://schemas.autonomy.com/aci/">
    <autn:reference>v:/20091015/XxjdzbC000177_20091015_BJTFN1.xml</autn:reference>
    <autn:id>630351</autn:id>
    <autn:section>0</autn:section>
    <autn:weight>96.00</autn:weight>
    <autn:cluster>2</autn:cluster>
    <autn:clustertitle>博览会, 即日, 奢侈品, 限量</autn:clustertitle>
    <autn:database>XINHUA</autn:database>
    <autn:title>(服务·消费)世界顶级奢侈品年底南京汇展</autn:title>
  </autn:hit>

   全部就不都列出来了

 

 

  我想要的效果就是取出  <autn:title>(服务·消费)世界顶级奢侈品年底南京汇展</autn:title>  的文本了
 

  效果图如下:

   xml读取代码如下:

 

       DataTable dt = new DataTable();//新建一个DataTable 
        dt.Columns.Add("items", System.Type.GetType("System.String"));
        xmldoc.Load(Server.MapPath("Hotspot.xml"));//加载xml
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);//命名空间类
        nsmgr.AddNamespace("autn",
http://schemas.autonomy.com/aci/);  //匹配xml的命名空间
        foreach (XmlNode node in xmldoc.SelectSingleNode("//*", nsmgr))//获取xml所有内容
        {
            if (node.Name == "autn:hit" || node.ParentNode.Name == "DOCUMENT")
            {
                DataRow dr = dt.NewRow();
                dr["items"] += node.LastChild.InnerText;
                dt.Rows.Add(dr);
            }
        }
        dlhotspot.DataSource = dt;//datalist显示
        dlhotspot.DataBind();

 

 

  xml的添加代码如下:

 

                xmldoc = new XmlDocument();
                xmldoc.Load(Server.MapPath("Hotspot.xml"));
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
                nsmgr.AddNamespace("autn", "
http://schemas.autonomy.com/aci/");
                XmlNode RootNode = xmldoc.CreateNode(XmlNodeType.Element, "autn", "hit", nsmgr.LookupNamespace("autn"));
                xmldoc.SelectSingleNode("//*", nsmgr).PrependChild(RootNode);
                XmlNode nodereference = xmldoc.CreateNode(XmlNodeType.Element, "autn", "reference", nsmgr.LookupNamespace("autn"));
                nodereference.InnerText = returnValue;
                RootNode.AppendChild(nodereference);
                XmlNode nodeid = xmldoc.CreateNode(XmlNodeType.Element, "autn", "id", nsmgr.LookupNamespace("autn"));
                nodeid.InnerText = "111111";
                RootNode.AppendChild(nodeid);

 

  xml的删除代码如下:

 

        xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("Hotspot.xml"));//加载xml
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);//添加命名空间类
        nsmgr.AddNamespace("autn", "
http://schemas.autonomy.com/aci/");
        foreach (XmlNode node in xmldoc.SelectSingleNode("//*", nsmgr))
        {
            if (node.Name == "autn:hit" || node.ParentNode.Name == "DOCUMENT")
            {
                if (node.LastChild.InnerText == e.CommandArgument.ToString())
                {
                   node.ParentNode.RemoveChild(node);//要将父级的删除了 不然会留下autn:hit节点
                    break;
                }
            }
        }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值