【发布时间】:2011-06-27 06:14:01
【问题描述】:
有没有更好的方法来做到这一点? 我必须获得两个属性值。 XML 总是只有这两个属性。
我的 xml:
<Template name="filename.txt">
<Property name="recordSeparator">\r\n</Property>
<Property name="fieldCount">16</Property>
</Template>
林克:
var property = from template in xml.Descendants("Template")
select new
{
recordDelim = template.Elements("Property").Where(prop => prop.Attribute("name").Value == "recordSeparator")
.Select(f => new { f.Value }),
fieldCount = template.Elements("Property").Where(prop => prop.Attribute("name").Value == "fieldCount")
.Select(f => new { f.Value })
};
【问题讨论】:
-
请澄清您的问题以解释您要做什么。阅读tinyurl.com/so-hints
-
您是否期望单个模板中有多个分隔符或 fieldCount 属性?因为,Select() 返回 IEnumerable
,在您的情况下为 IEnumerable 。这意味着 recordDelim 和 fieldCount 都将是 IEnumerable 类型。这是您在匿名对象中所需要的吗? -
@kornelijepetak,xml 总是相同的,只有这两个属性。