【问题标题】:How can i get the parent of node text value in c#?如何在 c# 中获取节点文本值的父级?
【发布时间】:2012-05-14 22:38:38
【问题描述】:

我有以下 xml:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

我找到了名为“午夜雨”的标题。现在我想知道他的父母是谁,这样我就可以使用&lt;author&gt; 文本节点。我试过类似的东西:

  var xpath = "../*[local-name() != 'title']";
       xml.Load(xmlalltext);
       var xl1 = xml.SelectNodes(xpath);
       MessageBox.Show(xl1.Item(0).InnerText.ToString());

【问题讨论】:

    标签: c# xml xpath selectnodes


    【解决方案1】:

    如果你已经找到了title节点并且正在寻找父节点,你可以选择当前节点的父节点。

    var parentNode = titleNode.SelectSingleNode("..");
    

    如果您正在寻找作者节点:

    var authorNode = titleNode.SelectSingleNode("../author");
    

    或者,您可以查找前面或后面的兄弟姐妹:

    var authorNode = titleNode.SelectSingleNode("following-sibling::author") ?? titleNode.SelectSingleNode("preceding-sibling::author");
    

    编辑:回答你的评论,如果你只有标题的字符串,那么你可以使用以下来获取作者:

    string xml = @"xml...";
    var doc = XDocument.Parse(xml);
    string author = doc
        .Descendants("book")
        .Where(x => x.Element("title").Value == "Midnight Rain")
        .Select(x => x.Element("author").Value)
        .FirstOrDefault();
    

    【讨论】:

    • 我只有字符串“Midnight Rain”,我想搜索他的作者。我确实了解您的代码的逻辑。谢谢!
    【解决方案2】:

    使用 Xlinq

    sample.xml(在下面的截图中)包含 xml 数据

            XElement root = XElement.Parse(File.ReadAllText("sample.xml"));
    
            var matchingBooks = root.Descendants().Where(i=>String.Equals(i.Value,"Midnight Rain")).Select(i=>i.Parent) ;
            var authors = matchingBooks.Elements("author");
    

    LinqPad 中的输出

     matchingBooks.Dump();
    
    authors.Dump();
    
    <book id="bk102">
            <author>Ralls, Kim</author>
            <title>Midnight Rain</title>
            <genre>Fantasy</genre>
            <price>5.95</price>
            <publish_date>2000-12-16</publish_date>
            <description>A former architect battles corporate zombies, 
                an evil sorceress, and her own childhood to become queen 
                of the world.</description>
            </book>
    

    <author>Ralls, Kim</author>
    

    【讨论】:

      【解决方案3】:

      您可以使用此 xpath expession 来获取作者节点

      "/catalog/book[title='Midnight Rain']/author"
      

      或者这个来获取父节点

      "/catalog/book[title='Midnight Rain']"
      

      例如。

       var result = xml.SelectNodes(string.Format("/catalog/book[title='{0}']/author", "Midnight Rain"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-22
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多