Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能得到林克的后代?_C#_Xml_Linq - Fatal编程技术网

C# 我怎样才能得到林克的后代?

C# 我怎样才能得到林克的后代?,c#,xml,linq,C#,Xml,Linq,编辑:所以我有一个需要解析的XML文件。经过研究,我已经能够得到没有问题的第一组节点,我非常喜欢返回它们的方式 我已经能够得到的直系子。但是获取下一个子体(&您需要这样做: from p in xml.Descendants("Project") from t in p.Descendants("tree") // selecting descendant node tree of Project where p.Attribute("AccessTag").Value == Trigger

编辑:所以我有一个需要解析的XML文件。经过研究,我已经能够得到没有问题的第一组节点,我非常喜欢返回它们的方式


我已经能够得到
的直系子。但是获取下一个子体(
&
您需要这样做:

from p in xml.Descendants("Project")
from t in p.Descendants("tree") // selecting descendant node tree of Project
  where p.Attribute("AccessTag").Value == Trigger
  select new {
    title = p.Element("Title").Value,
    reader = p.Element("ReaderURL").Value,
    status = p.Element("Status").Value,
    branch = t.Element("branch").Value // select value here
    }).Single();

您需要这样做:

from p in xml.Descendants("Project")
from t in p.Descendants("tree") // selecting descendant node tree of Project
  where p.Attribute("AccessTag").Value == Trigger
  select new {
    title = p.Element("Title").Value,
    reader = p.Element("ReaderURL").Value,
    status = p.Element("Status").Value,
    branch = t.Element("branch").Value // select value here
    }).Single();
试试这个:-

var result = xml.Descendants("Project")
           .Where(x => (string)x.Attribute("AccessTag") == "Trigger")
           .Select(x => new
  {
     Title = (string)x.Element("Title"),
     ReaderURL = (string)x.Element("ReaderURL"),
     Status = (string)x.Element("Status"),
     Branch = x.Descendants("tree") //Here are fetching the Descendants
              .Select(z => (string)z.Element("branch")).FirstOrDefault()
   }).FirstOrDefault();
请注意,我在获取分支时使用了
FirstOrDefault
,因为我认为您只需要第一个分支元素,如果不是这样,请将FirstOrDefault替换为
ToList()
,它将返回所有分支元素。

尝试以下操作:-

var result = xml.Descendants("Project")
           .Where(x => (string)x.Attribute("AccessTag") == "Trigger")
           .Select(x => new
  {
     Title = (string)x.Element("Title"),
     ReaderURL = (string)x.Element("ReaderURL"),
     Status = (string)x.Element("Status"),
     Branch = x.Descendants("tree") //Here are fetching the Descendants
              .Select(z => (string)z.Element("branch")).FirstOrDefault()
   }).FirstOrDefault();

请注意,我在获取分支时使用了
FirstOrDefault
,因为我认为您只需要第一个分支元素,如果不是这样,请将FirstOrDefault替换为
ToList()
,它将返回所有分支元素。

您可以使用元素而不是子体

var root = xml.Element("Projects").Element("Project")
              .Where(x => (string)x.Attribute("AccessTag") == "Trigger")
              .Select(x => {...});

您可以使用元素而不是子体

var root = xml.Element("Projects").Element("Project")
              .Where(x => (string)x.Attribute("AccessTag") == "Trigger")
              .Select(x => {...});

当你说后代时,你指的是
和/或
分支
节点的值?是的,这就是我的意思。为了清晰起见进行了更新。当你说后代时,你指的是
和/或
分支
节点的值?是的,这是我的意思。为了清晰起见进行了更新。假设整个X中只有1个
ML,这很好。@Tony Arnold你的意思是什么?如果有多个树的子节点。比如
@Tony Arnold删除
single()的调用
,然后它将获得该
节点的所有
子节点。如果整个XML中只有一个
,这就可以了。@Tony Arnold你的意思是什么?如果有多个树子节点。比如
@Tony Arnold删除
single()的调用
,然后它将获得该
节点的所有
子节点