Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# Linq到XML从节点语句简单获取属性_C#_Linq To Xml - Fatal编程技术网

C# Linq到XML从节点语句简单获取属性

C# Linq到XML从节点语句简单获取属性,c#,linq-to-xml,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.Att

下面是代码片段:

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;
下面是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>

我做错了什么?我不断得到一个“对象未设置为对象实例”异常


我试图返回的是基于父节点类型的所选节点的类型,即,如果主题为“敏捷”,父节点为“项目”,则返回值应为“迭代”。这是最后的结果,但我从来没有走那么远,因为我被你上面看到的东西困住了。

我不清楚你的问题。这是我的解决方案(据我所知)


您的查询中有两个错误

第一个是:

from t in themes.Descendants()
这将为您提供所有
主题
元素。
如果需要
主题
元素,则必须通过如下过滤方式指定它:

from t in themes.Descendants("theme")
因此,第二个错误是

select new { value = t.Attribute("type").Value }
因为
t
将是一个
theme
元素,没有属性“type”。
在这一点上我无能为力,因为还不清楚最终结果是什么

编辑:根据添加的信息,这应该可以做到:

        var childType =
            from t in doc.Descendants("theme")
            where t.Attribute("name").Value.Equals(theme)
            select t.Element("root")
                    .Element("node")
                    .Attribute("type").Value;
但是,这意味着要从具有相同
主题的节点中检索多个类型。

如果类型总是相同的,则应该考虑调用<代码> .SunLeOrdRebug()/<代码> < /P> < P>我想你想更接近这个:

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;
编辑: 根据您的其他信息,这可能更接近:

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

        }
static void Main(字符串[]args)
{
var xml=“”;
XDocument themes=XDocument.Parse(xml);
字符串结果=”;
var theme=“敏捷”;
var parent=“项目”;
var childType=来自themes.substands(“theme”)中的t
其中t.Attribute(“name”).Value.Equals(主题)
其中t.Element(“root”).Attribute(“type”).Value==父级
来自t.Element(“根”).subjects()中的子元素
选择新的{value=children.Attribute(“type”).value};
foreach(childType中的变量t)
{
控制台写入线(t值);
}
Console.Read();
}

好的,这是我最后做的,它不太漂亮,但很管用。 这是基于Pramodh的回答和几个tweek

string result = "";
                var childType = themes.Descendants("theme")
                                               .Where(x => x.Attribute("name").Value == theme)
                                               .Where(x => x.Descendants("node").First().Attribute("type").Value == parentType)
                                               .Select(x => x.Descendants("node").First().Descendants().First().Attribute("type").Value);

                foreach (var t in childType) {
                    result += t;
                }
                return result;
现在,若我传入主题“敏捷”和父级“迭代”,它将返回一个故事

就像我说的,不是很漂亮,但很管用


感谢所有发布答案的人。

FirstNode的方法版本中没有一个属性没有属性方法。我必须在select语句中向t.Descents()添加.First()以使其编译,但是,我仍然将对象未设置为对象例外的实例。您的第二次编辑将为我返回空白值。我无法处理xml的布局,是吗?我已经再次更新,根据您给出的内容显示了完整的工作示例。如果它仍然不起作用,请查看您的xml并确保它的格式与我的格式相同。除非xml中还有其他内容,否则它应该对您有用。我编辑了我的答案,以与您添加的信息保持一致。
string result = "";
                var childType = themes.Descendants("theme")
                                               .Where(x => x.Attribute("name").Value == theme)
                                               .Where(x => x.Descendants("node").First().Attribute("type").Value == parentType)
                                               .Select(x => x.Descendants("node").First().Descendants().First().Attribute("type").Value);

                foreach (var t in childType) {
                    result += t;
                }
                return result;