XmlDocument加载有Xmlns的xml文档,使用Xpath

using System;
using System.IO;
using System.Xml; public class Sample
{
public static void Main()
{ XmlDocument doc = new XmlDocument();
doc.Load("newbooks.xml"); // Create an XmlNamespaceManager to resolve the default namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema"); // Select the first book written by an author whose last name is Atwood.
XmlNode book;
XmlElement root = doc.DocumentElement;
book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr); Console.WriteLine(book.OuterXml); }
}
<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
<book genre="novel" style="hardcover">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" style="other">
<title>The Poisonwood Bible</title>
<author>
<first-name>Barbara</first-name>
<last-name>Kingsolver</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
上一篇:MySQL学习【第十篇存储引擎实际应用】


下一篇:HTML5之webSocket使用