Linq to XML简单从节点语句获取属性

Mor*_*geh 2 c# linq-to-xml

这是代码片段:

XDocument themes = XDocument.Load(HttpContext.Current.Server.MapPath("~/Models/Themes.xml"));
string result = "";
var childType = from t in themes.Descendants()
    where t.Attribute("name").Value.Equals(theme)
    select new { value = t.Attribute("type").Value };

foreach (var t in childType) {
    result += t.value;
}
return result;
Run Code Online (Sandbox Code Playgroud)

这是XML:

<?xml version="1.0" encoding="utf-8" ?>
<themes>
  <theme name="Agile">
    <root type="Project">
      <node type="Iteration" >
        <node type="Story">
          <node type="Task"/>
        </node>
      </node>
    </root>
  </theme>
  <theme name="Release" >
    <root type="Project">
      <node type="Release">
        <node type="Task" />
        <node type="Defect" />
      </node>
    </root>
  </theme>
</themes>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我一直得到一个"未设置为对象实例的对象"异常.

我想要返回的是基于父节点类型的所选节点的类型,即,如果主题是"敏捷"而父节点是"项目",则返回值应为"迭代".这是最后的结果,但我从来没有这么远,因为我坚持你上面看到的.

Chr*_*way 5

我想你想要更接近这个:

XDocument themes = XDocument.Load(HttpContext.Current.Server.MapPath("~/Models/Themes.xml"));
string result = "";
var childType = from t in themes.Descendants("theme")
                where t.Attribute("name").Value.Equals(theme)
                select new { value = t.Descendants().Attribute("type").Value };

foreach (var t in childType) {
    result += t.value;
}
return result;
Run Code Online (Sandbox Code Playgroud)

编辑: 根据您的其他信息,也许这更接近:

XDocument themes = XDocument.Load(HttpContext.Current.Server.MapPath("~/Models/Themes.xml"));
string result = "";
var childType = from t in themes.Descendants("theme")
                where t.Attribute("name").Value.Equals(theme)
                where t.Element("node").Attribute("type").Value == parent
                select new { value = t.Descendants().Attribute("type").Value };

foreach (var t in childType) {
    result += t.value;
}
return result;
Run Code Online (Sandbox Code Playgroud)

编辑2:这对我有用:

XDocument themes = XDocument.Load(HttpContext.Current.Server.MapPath("~/Models/Themes.xml"));
string result = "";
var theme = "Agile";
var parent = "Project";
var childType = from t in themes.Descendants("theme")
            where t.Attribute("name").Value.Equals(theme)
            where t.Element("root").Attribute("type").Value == parent
            from children in t.Element("root").Descendants()
            select new { value = children.Attribute("type").Value };
foreach (var t in childType) {
    result += t.value;
}
return result;
Run Code Online (Sandbox Code Playgroud)

编辑3:这是完整的工作程序,只需将它放在控制台应用程序的类中.

static void Main(string[] args)
        {
            var xml = "<themes><theme name='Agile'><root type='Project'><node type='Iteration' ><node type='Story'><node type='Task'/></node></node></root></theme><theme name='Release' ><root type='Project'><node type='Release'><node type='Task' /><node type='Defect' /></node></root></theme></themes>";
            XDocument themes = XDocument.Parse(xml);
            string result = "";
            var theme = "Agile";
            var parent = "Project";
            var childType = from t in themes.Descendants("theme")
                            where t.Attribute("name").Value.Equals(theme)
                            where t.Element("root").Attribute("type").Value == parent
                            from children in t.Element("root").Descendants()
                            select new { value = children.Attribute("type").Value };

            foreach (var t in childType)
            {
                Console.WriteLine(t.value);
            }

            Console.Read();

        }
Run Code Online (Sandbox Code Playgroud)