net处理有命名空间的xml文件的方法
xml文件
C#代码
string path = MapPath("~/app_data/XMLFile.xml");
XPathDocument document = new XPathDocument(path);
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager man = new XmlNamespaceManager(navigator.NameTable);
man.AddNamespace("bk1", "http://www.contoso.com/books");
XPathExpression query = navigator.Compile("/bk1:bookstore/bk1:book/bk1:title");
query.SetContext(man);
// Select all books authored by Melville.
//XPathNodeIterator nodes = navigator.Select("descendant::book:book[book:author/last-name='Melville']",man);
// string query="/bk:bookstore/bk:book/bk:title";
XPathNodeIterator nodes = navigator.Select(query);
while (nodes.MoveNext())
{
// Clone the navigator returned by the Current property.
// Use the cloned navigator to get the title element.
XPathNavigator clone = nodes.Current.Clone();
clone.MoveToFirstChild();
Response.Write(string.Format("Book title: {0}", clone.Value));
Response.Write("<Br>");
}
<?xml version="1.0" encoding="utf-8" ?>
<bk:bookstore xmlns:bk="http://www.contoso.com/books">
<bk:book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<bk:title>The Autobiography of Benjamin Franklin</bk:title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</bk:book>
<bk:book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<bk:title>2222</bk:title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</bk:book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bk:bookstore>
<bk:bookstore xmlns:bk="http://www.contoso.com/books">
<bk:book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<bk:title>The Autobiography of Benjamin Franklin</bk:title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</bk:book>
<bk:book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<bk:title>2222</bk:title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</bk:book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bk:bookstore>
C#代码
string path = MapPath("~/app_data/XMLFile.xml");
XPathDocument document = new XPathDocument(path);
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager man = new XmlNamespaceManager(navigator.NameTable);
man.AddNamespace("bk1", "http://www.contoso.com/books");
XPathExpression query = navigator.Compile("/bk1:bookstore/bk1:book/bk1:title");
query.SetContext(man);
// Select all books authored by Melville.
//XPathNodeIterator nodes = navigator.Select("descendant::book:book[book:author/last-name='Melville']",man);
// string query="/bk:bookstore/bk:book/bk:title";
XPathNodeIterator nodes = navigator.Select(query);
while (nodes.MoveNext())
{
// Clone the navigator returned by the Current property.
// Use the cloned navigator to get the title element.
XPathNavigator clone = nodes.Current.Clone();
clone.MoveToFirstChild();
Response.Write(string.Format("Book title: {0}", clone.Value));
Response.Write("<Br>");
}