C#如何提取完整的XML节点集 [英] C# How to extract complete xml node set
本文介绍了C#如何提取完整的XML节点集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<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">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
时的任何方式使用XPath来选择完整的第一个节点集,例如来自
Is their any way using XPath to select the complete first node set, for example from
<book category="COOKING">
to
</book>,
这样,那大块XML可以存储供以后使用。
so that, that chunk of xml can be stored for later use.
鲍勃。
推荐答案
比方说,这个XML存储在的XmlDocument
名为商务部
。
Let's say this XML is stored in an XmlDocument
called doc
.
XmlElement docRoot = doc.DocumentElement;
XmlNode cookingNode = docRoot.SelectSingleNode("./book[@category='COOKING']");
我测试了这一点,并添加了此行来验证:
I tested this and added this line to verify:
Console.WriteLine(cookingNode.OuterXml);
下面是输出:
<book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada
De Laurentiis</author><year>2005</year><price>30.00</price></book>
这篇关于C#如何提取完整的XML节点集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文