我有這樣的LINQ查詢:的LINQ to XML不知道節點
XNamespace ns = NAMESPACE;
var items = (from c in doc.Descendants(ns +"Item")
select new Item
{
Title = c.Element(ns + "ItemAttributes").Element(ns + "Title").Value,
MFR = c.Element(ns + "ItemAttributes").Element(ns + "Manufacturer").Value,
Offer = c.Element(ns + "Offers").Element(ns + "TotalOffers").Value,
Amazon = c.Element(ns + "Offer").Element(ns + "Merchant").Elements(ns + "MerchantId"),
LowPrice = Convert.ToDouble(c.Element(ns + "FormattedPrice").Value),
SalesRank = Convert.ToInt32(c.Element(ns +"SalesRank").Value),
ASIN = c.Element(ns + "ASIN").Value
}).ToList<Item>();
它的偉大工程期待當一個節點不存在。例如,我沒有MFR或銷售排名。我該如何做到這一點,如果它沒有問題的節點,它給了我一個默認值或在我不試圖抓住我的整個查詢一個項目。
對不起,原來的代碼片段,我張貼是不完整的。我已經編輯完整的擴展名。 –
我更改爲'existingElement = new XElement(name,string.Empty);''existingElement = new XElement(name,「0」);'它對我的需求非常好! –
真棒,很高興它幫助! –