这是 XML 大纲:
<Root>
<Thing att="11">
<Child lang="e">
<record></record>
<record></record>
<record></record>
</Child >
<Child lang="f">
<record></record>
<record></record>
<record></record>
</Child >
</Thing>
</Root>
我有以下内容:
TextReader reader = new StreamReader(Assembly.GetExecutingAssembly()
.GetManifestResourceStream(FileName));
var data = XElement.Load(reader);
foreach (XElement single in Data.Elements())
{
// english records
var EnglishSet = (from e in single.Elements("Child")
where e.Attribute("lang").Equals("e")
select e.Value).FirstOrDefault();
}
但是我什么也得不到。我希望能够为每个“事物”选择属性“lang”等于一个值的“子项”。
这个我也试过了,没用。
var FrenchSet = single.Elements("Child")
.Where(y => y.Attribute("lang").Equals("f"))
.Select(x => x.Value).FirstOrDefault();
请您参考如下方法:
您正在检查 XAttribute 对象是否等于字符串 "e"。
由于 XAttribute 对象永远不会等于字符串,因此它不起作用。
您需要检查 XAttribute 对象的 Value,如下所示:
where y => y.Attribute("lang").Value == "e"
