Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图在Windows Phone 7.8应用程序中解析xml,但得到空值?_C#_Visual Studio 2010_Windows Phone 7_Xml Parsing_Linq To Xml - Fatal编程技术网

C# 试图在Windows Phone 7.8应用程序中解析xml,但得到空值?

C# 试图在Windows Phone 7.8应用程序中解析xml,但得到空值?,c#,visual-studio-2010,windows-phone-7,xml-parsing,linq-to-xml,C#,Visual Studio 2010,Windows Phone 7,Xml Parsing,Linq To Xml,我正在尝试解析作为对webservice响应的响应而得到的xml。xml如下所示 <GeneralSearchResponse> <serverDetail></serverDetail> <exceptions exceptionCount="1"></exceptions> <clientTracking height="19" type="logo" width="106"></clientTracking>

我正在尝试解析作为对webservice响应的响应而得到的xml。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>

但xEle没有任何类别。请告诉我问题出在哪里???

您的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");

您的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");

您确定这就是确切的XML吗?不涉及名称空间?是的,有名称空间xmlns=“urn:types.partner.api.shopping.com”,那么这就是问题所在。但是,请在将来让您的问题与您的问题实际匹配,这样可以减少所涉及的猜测。您确定这是确切的XML吗?不涉及名称空间?是的,有名称空间xmlns=“urn:types.partner.api.shopping.com”那么这就是问题所在。但请在将来让你的问题与你的问题相匹配——这样可以减少所涉及的猜测。