0

我試圖解析XML,我得到的迴應web服務響應的XML如下所示嘗試解析Windows Phone 7.8應用程序中的xml,但得到空值?

<GeneralSearchResponse> 
<serverDetail></serverDetail> 
<exceptions exceptionCount="1"></exceptions> 
<clientTracking height="19" type="logo" width="106"></clientTracking> 
<searchHistory></searchHistory> 
<categories matchedCategoryCount="1" returnedCategoryCount="1"> 
<category id="0"> 
<name>bart</name> 
<categoryURL>http://www.shopping.com/bart/products?oq=bart&linkin_id=7000610 
</categoryURL> 
<items matchedItemCount="1045" pageNumber="1" returnedItemCount="5"> 
<product id="130506037"></product> 
<product id="104483377"></product> 
<offer featured="false" id="tp-VCdOoO1RL6xICeRONqg==" smartBuy="false" used="false"></offer> 
<offer featured="false" id="12evWWi57lddzFufngUWsg==" smartBuy="false" used="false"></offer> 
<product id="96754577"></product> 
</items> 
<attributes matchedAttributeCount="5" returnedAttributeCount="5"></attributes> 
<contentType>hybrid</contentType> 
</category> 
<intActualCategoryCount>4</intActualCategoryCount> 
</categories> 
<relatedTerms></relatedTerms> 
</GeneralSearchResponse> 

但是,當我試圖解析使用下面的代碼我無法得到任何下降或任何節點

XDocument xDoc = new XDocument(); 
xDoc = XDocument.Parse(data); 
var xEle = xDoc.Root.Descendants("categories"); 

但是xEle沒有任何類別。請讓我知道是什麼問題?

+0

你確定這是*確切的* XML - 沒有名稱空間參與? – 2013-04-05 21:04:41

+0

是的,有命名空間xmlns =「urn:types.partner.api.shopping.com」 – 2013-04-05 21:08:37

+0

那麼這就是問題了。但請在將來讓你的問題真正符合你的問題 - 它減少了涉及的猜測。 – 2013-04-05 21:09:24

回答

4

你的XML有一個默認的命名空間 - 所以它的元素都在這個命名空間中。在LINQ to XML中查找元素的方法是名稱空間敏感的。你想要:

XNamespace ns = "urn:types.partner.api.shopping.com"; 
XDocument xDoc = new XDocument(); 
xDoc = XDocument.Parse(data); 
var xEle = xDoc.Root.Descendants(ns + "categories");