如何在c#中获取节点文本值的父级?

时间:2021-10-04 15:05:27

I have the following xml:

我有以下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>

I found the title named "Midnight Rain". Now i want to know who is his parent so that i can use the <author> text node. I tried something like:

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

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

3 个解决方案

#1


3  

If you've already found the title node and you're looking for the parent node, you can just select the parent node of the current node.

如果您已找到标题节点并且正在查找父节点,则只需选择当前节点的父节点即可。

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

If you're looking for the author node:

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

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

Alternatively, you may look for preceding or following siblings:

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

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

Edit: To answer your comment, if you only have the string of the title, then you may use the following to get the 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();

#2


1  

using XLinq

使用XLinq

sample.xml(in below snipped) contains xml data

sample.xml(在下面的snipped中)包含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");

Output in LinqPad

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


0  

You can use this xpath expession to get author node

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

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

or this to get parent node

或者这个来获取父节点

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

eg.

例如。

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

#1


3  

If you've already found the title node and you're looking for the parent node, you can just select the parent node of the current node.

如果您已找到标题节点并且正在查找父节点,则只需选择当前节点的父节点即可。

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

If you're looking for the author node:

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

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

Alternatively, you may look for preceding or following siblings:

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

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

Edit: To answer your comment, if you only have the string of the title, then you may use the following to get the 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();

#2


1  

using XLinq

使用XLinq

sample.xml(in below snipped) contains xml data

sample.xml(在下面的snipped中)包含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");

Output in LinqPad

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


0  

You can use this xpath expession to get author node

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

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

or this to get parent node

或者这个来获取父节点

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

eg.

例如。

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