如果用selectNodes方法搜索节点时,源文件中的命名空间默认命名空间的方法

源文件 books.xml

<?xml version="1.0" encoding="utf-8"?> The Autobiography of Benjamin Franklin Benjamin Franklin price < 8.99 The Confidence Man Herman Melville 11.99 The Gorgias Plato 9.99 执行函数: XmlDocument doc = new XmlDocument(); doc.Load(@"../../books.xml"); XmlNamespaceManager nm = new XmlNamespaceManager(doc.NameTable); //在books.xml文件中只有默认的xmlns命名空间没有指定的名称,但是在XmlNamespaceManager 中的需要将Addnamespace的第一个参数指定一个值,然后在selectnodes中加上命名空间的前缀 nm.AddNamespace("ru", "http://microsoft.com/schemas/VisualStudio/TeamTest/2010");//第一个参数ru为指定的前缀,源文件中可以没有
           XmlNodeList nodes = doc.DocumentElement.SelectNodes("//ru:book",nm);//在使用xpath搜索的时候需要用前缀:元素名的格式即ru:book
          //  XmlNodeList nodes = doc.DocumentElement.SelectNodes("//book");
            Console.WriteLine(nodes.Count);
            for( int i = 0;i< nodes.Count;i++)
            {
               //nodes[i].FirstChild.InnerText += "I will change this node";
                //Console.WriteLine(nodes[i].NamespaceURI);
                Console.WriteLine(nodes[0].FirstChild.Name);
            }
            doc.Save(Console.Out);
            Console.WriteLine();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值