【问题标题】:C# How to extract complete xml node setC#如何提取完整的xml节点集
【发布时间】:2009-10-02 19:30:38
【问题描述】:
<?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 就可以存储起来供以后使用。

鲍勃。

【问题讨论】:

    标签: c# xpath c#-3.0


    【解决方案1】:

    假设这个 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>
    

    【讨论】:

      【解决方案2】:

      此查询将选择该节点。您是想获得一组节点还是一个节点?如果您只想要节点的第一个子集,您可能必须自己放回书店节点。

      /bookstore/book[@category='COOKING']
      

      作为 XmlDocument ...

      var x = new XmlDocument();
      x.Load("XmlFile1.xml");
      var ns = x.SelectSingleNode("/bookstore/book[@category='COOKING']");
      
      var res = ns.OuterXml;
      

      作为 XDocument ...

      var x = XDocument.Load("XmlFile1.xml");
      
      var root = new XElement("bookstore",
          from book in x.Element("bookstore").Elements("book")
          where book.Attribute("category").Value == "COOKING"
          select book
          );
      

      如果你只想要 book 节点,你可以这样做而不是上面的根版本

      var book = x.Element("bookstore")
          .Elements("book")
          .Where(n => n.Attribute("category").Value == "COOKING")
          .First();
      

      【讨论】:

      • 我试过了。那只会带回“烹饪节点”。我想要的是将特定块内的所有内容从 带回 ...
      • 我正在读取进入消息管道的 xml 消息。我需要存储它们的特定部分,以便这些部分可以构建成一个新的复合消息,本质上是一个请求/回复。所以我需要一种方法来从传入的消息中挑选选定的 xml 块,以便以后用于构建回复消息。
      • 我正在使用 SelectNode 和 SelectSingleNode 获取整个节点。 Xml.Linq 类的工作方式相同。
      • @scope-creep:从var ns = x.SelectSingleNode([...]),您应该能够使用ns.ChildNodes 访问该节点内的节点。
      • 您还可以检查 .OuterXml,它会显示您所在的节点。如果您使用 .InnerXML,它只会向您显示孩子。
      【解决方案3】:

      假设我只想提取xml文件如下的数据..

      <book category="COOKING"> 
          <title lang="en">Everyday Italian</title>
          <author auth="up">Giada De Laurentiis</author>    
          <year>2005</year>
         <price>30.00</price>
        </book>
      

      列表视图上的最终结果应该是这样的

        lang       auth
        en          up
      

      我的编码如下..

      XmlNodeList elemList = doc.GetElementsByTagName("book");
                          for (int j = 0; j < elemList.Count; j++)
                          {
                              if (elemList[j].Attributes["category"].Value == "COOKING")
                              {
                                  XmlNodeList elemList1 = doc.GetElementsByTagName("author");
                                  for (int i = 0; i < elemList1.Count; i++)
                                  {
                                      string attrVal = elemList1[i].Attributes["lang"].Value;
                                      string attrVal1 = elemList1[i].Attributes["auth"].Value;
      
                                      ListViewItem lvi = new ListViewItem();
      
                                          lvi.SubItems.Add(attrVal1);
                                          lvi.SubItems.Add(attrVal1);
                                      }
                                      listView1.Items.Add(lvi);
                                  }
                              }
                          }
      

      【讨论】:

      • 最后我有我的要求的代码..XmlDocument xDoc = new XmlDocument(); // (Put code to populate xDoc here) XmlNodeList xNode = xDoc.SelectNodes(@"/bookstore/book[@category='COOKING']/title");
      【解决方案4】:

      添加到马修的回应中:

      XmlDocument xDoc = new XmlDocument();
      // (Put code to populate xDoc here)
      XmlNodeList xNode = xDoc.SelectNodes(@"/bookstore/book[@category='COOKING']");
      

      xNode 现在等于 COOKING 类型的 Book。

      【讨论】:

      • 我认为这会给你一个XmlNodeList 而不是XmlNode
      • 是的...您需要获取第一个索引或使用 SelectSingleNode
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2017-11-15
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多