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