我怎样才能得到节点文本值在C#中父? [英] How can i get the parent of node text value in c#?

查看:179
本文介绍了我怎样才能得到节点文本值在C#中父?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:<?/ p>

 < XML版本=1.0> 
<目录和GT;
<书ID =bk101>
<作者> Gambardella,马修< /笔者>
<标题> XML开发人员指南< /标题>
<&流派GT;计算机与LT; /流派>
<价格>&44.95 LT; /价格>
<出版日期>&2000-10-01 LT; /出版日期>
<描述方式>的深入看一下使用XML创建应用程序
< /描述>
< /书>
<书ID =bk102>
<作者>罗尔斯,金< /笔者>
<标题>三更雨< /标题>
<&流派GT;&幻想LT; /流派>
<价格>&5.95 LT; /价格>
<出版日期>二零零零年十二月十六日< /出版日期>
<描述>在原建筑师战役企业僵尸,
一个邪恶的女巫,而她自己的童年,成为世界的女王
< /描述>
< /书>
< /目录>



我发现一个名为三​​更雨的称号。现在我想知道谁是他的父母,这样我可以使用<作者> 文本节点。我想是这样的:

  VAR的XPath =../* [!本地名称()='标题'] ; 
与XML.load(xmlalltext);
VAR XL1 = xml.SelectNodes(XPath的);
MessageBox.Show(xl1.Item(0).InnerText.ToString());


解决方案

如果你已经找到了标题节点,你要找的父节点,你可以选择当前节点的父节点。

  VAR parentNode = titleNode.SelectSingleNode (); 

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

  VAR authorNode = titleNode.SelectSingleNode(../作者); 



另外,你可以寻找前面或后面的兄弟姐妹:

  VAR authorNode = titleNode.SelectSingleNode(以下同胞::作者)? titleNode.SelectSingleNode(前同辈作家::); 



编辑:要回答你的评论,如果你只有标题的字符串,那么你可以使用下面让笔者:

 字符串XML = @XML ...; 
VAR DOC = XDocument.Parse(XML);
串笔者= DOC
.Descendants(书)
。凡(X =方式> x.Element(标题)值==三更雨)
。选择(X =方式> x.Element(作家)值)
.FirstOrDefault();


I have the following 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());

解决方案

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();

这篇关于我怎样才能得到节点文本值在C#中父?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆