【发布时间】:2014-10-09 04:45:30
【问题描述】:
这里是sn-p的代码:
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>
我做错了什么?我不断收到“对象未设置为对象的实例”异常。
我要返回的是基于父节点类型的所选节点的类型,即如果主题是“敏捷”并且父节点是“项目”,那么返回值应该是“迭代”。这是最终的结果,但我从来没有走到那一步,因为我被你在上面看到的东西困住了。
【问题讨论】:
-
我编辑了我的答案以符合您添加的信息。
标签: c# linq-to-xml