var subset = from item in document.Descendants("Id")
where item.Value == itemId.ToString()
select new PurchaseItem() {
Id = int.Parse(item.Parent.Element("Id").Value),
Name = item.Parent.Element("Name").Value,
Description = item.Parent.Element("Description").Value,
Price = int.Parse(item.Parent.Element("Price").Value)
};
的XML的结构如下:Linq-XML总是如此混乱吗?
<Items>
<Item>
<Id></Id>
<Name></Name>
<Description></Description>
<Price></Price>
</Item>
</Items>
标识,和价格都是整数值。名称和描述是字符串。
我发现Linq to XML
非常适合我使用它,这只是一个片段。但另一方面,我感觉它应该或可能更清洁。该演员似乎是这个片段中最明显的问题。
有什么建议吗?
尝试使用XmlReader,甚至是旧的XML DOM,你将重新评估你的'凌乱'的观点。 ;) – Noldorin 2010-01-28 22:26:04
这里的语法在动态方面会非常干净。想象一下'ID = item.Id'等 – 2010-01-28 22:26:11
@Noldorin - 'untidy'会比较适合;)我过去使用过XMLReader,现在很乱。 – Finglas 2010-01-28 22:37:10