如何从xml文件中选择节点?

如何从xml文件中选择节点?,xml,asp-classic,Xml,Asp Classic,下面是我的xml文件 <?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="cooking"> <book> <bookID>1111</bookID> </book> <title lang="en">Everyday Italian</title> <author>Giada De L

下面是我的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>

<book category="cooking">
<book>
    <bookID>1111</bookID>
</book>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="children">
<book>
    <bookID>54655</bookID>
</book>
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="web">
<book>
    <bookID>5556</bookID>
</book>
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>

<book category="web" cover="paperback">
<book>
    <bookID>1111</bookID>
</book>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>

使用System.Xml.Linq,这将是最简单的

将其加载到XElement中,并使用Linq查询它

XDocument test = XDocument.Load(xmlFilename); // load from string if you want
XElement cooking = test.Element("bookstore")
                       .Descendants("book")
                       .Where(e => e.Attribute("category" => "cooking")
                       .FirstOrDefault();

使用System.Xml.Linq,这将是最简单的

将其加载到XElement中,并使用Linq查询它

XDocument test = XDocument.Load(xmlFilename); // load from string if you want
XElement cooking = test.Element("bookstore")
                       .Descendants("book")
                       .Where(e => e.Attribute("category" => "cooking")
                       .FirstOrDefault();

如果不能像@sonjz建议的那样使用System.Xml.Linq,那么可以使用和XPath表达式

set nodes = xml.documentElement.selectNodes("//book/title")

for each node in nodes
    response.write node.text & "<br/>"
next
set nodes=xml.documentElement.selectNodes(“//book/title”)
对于节点中的每个节点
response.write node.text&“
” 下一个
如果无法按照@sonjz的建议使用System.Xml.Linq,则可以使用和XPath表达式

set nodes = xml.documentElement.selectNodes("//book/title")

for each node in nodes
    response.write node.text & "<br/>"
next
set nodes=xml.documentElement.selectNodes(“//book/title”)
对于节点中的每个节点
response.write node.text&“
” 下一个
-1这是一个ASP经典问题,XDocument/Linq不可用。注意,我记得XPath、selectNodes(获取列表)和selectSingleNode(首次出现)xml.documentElement.selectSingleNode(“//book[category='cooking']”)有一个奇怪的问题-1这是一个ASP经典问题,XDocument/Linq不可用。注意,我记得XPath、selectNodes(获取列表)和selectSingleNode(首次出现)xml.documentElement.selectSingleNode(“//book[category='cooking']”)有一件奇怪的事情