C# 如何提取完整的xml节点集

标签 c# xpath c#-3.0

<?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 选择完整的第一个节点集,例如从

 <book category="COOKING">  
  to 
 </book>, 

这样,该 xml block 就可以存储起来供以后使用。

鲍勃。

最佳答案

假设此 XML 存储在名为 docXmlDocument 中。

XmlElement docRoot = doc.DocumentElement;
XmlNode cookingNode = docRoot.SelectSingleNode("./book[@category='COOKING']");

我对此进行了测试并添加了这一行来验证:

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节点集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1511377/

相关文章:

xml - 如何计算具有相同属性值的元素

c# - 将 orderby、Skip() 和 Take() 与 LINQ 一起使用时出现重复行

attributes - 在没有现有文件的C#中创建一个空的FileInfo对象

.net - 使用 C# 中的文件路径复制文件夹中的文件

c# - LINQ OR 运算符不给出结果

c# - Excel 文档的 OpenXml SDK 2.0 中的卡住 Pane

java - 为什么在 IE 中应该更喜欢使用 CSS 而不是 XPath?

c# - 编码 LPSTR 和 float 时堆栈不平衡

c# - SignalR 客户端中心代理未定义

html - 使用 XPath 从 HTML 中提取信息