2011-12-23 79 views
2

我有一個XML Like Below,但我無法解析它。請幫我解析下面的XML。如何解析Windows Phone 7中的soap XML

<?xml version="1.0" encoding="utf-8"?><soap:Envelope  
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body> 
<GetResponse xmlns="http://tempuri.org/"><GetResult> 
<diffgr:diffgram> 
<NewDataSet> 
<Table> 
<a>hi1</a> 
<b>hi2</b> 
</Table> 
<Table> 
<a>hi3</a> 
<b>hi4</b> 
</Table> 
</NewDataSet> 
</diffgr:diffgram> 
</GetResponse></GetResult> 
</soap:Body> 
</soap:Envelope> 

這裏我想要表(即a,b)標記的結果。我嘗試使用Linq,但我無法解析它。我試了一下這樣的代碼:

//XML will be there in response string 
String response = e.response; 
public static String myNamespace = "http://tempuri.org/"; 
XDocument reader = XDocument.Parse(response); 
var results = from result in reader.Descendants(XName.Get("GetResponse", myNamespace)) 
       select result.Element("GetResult"). 

但是這段代碼返回null。

謝謝提前。

+4

那麼,爲什麼你不只是生成與模式的客戶端,讓淨爲您做解析? – Bashwork 2011-12-23 15:30:05

+0

謝謝Bashwork。但我想在Windows Phone 7中解析這個XML。我可以在WP7中解析這個XML。 – 2011-12-24 08:24:41

+0

@MahiKumar - 您可以在WP7項目中設置服務參考。或者你的意思是說應該是動態的,因爲模式每次都會有所不同? – 2011-12-24 08:38:58

回答

0

在您的c#項目中,您可以導入W​​eb引用,並將該引用指向您正在使用的Web服務提供者。這個引用將創建一個封裝類,它將把所有的SOAP協議抽象爲你。

請參閱MSDN鏈接查看更多細節:http://msdn.microsoft.com/en-us/library/d9w023sx%28VS.71%29.aspx

+0

感謝Carl T.我想在Windows Phone7中解析這個XML。我如何在WP7中解析這個XML。 – 2011-12-24 08:25:10

+0

-1:爲什麼在世界上你會發佈一個.NET 1.1鏈接作爲Windows Phone問題的答案?另外,爲什麼你會建議使用Web參考,這是傳統技術? – 2011-12-24 08:25:57

+0

我添加了服務引用並使用它,但是在服務中它們正在返回數據集,所以數據集不是可用的wp7。所以我必須使用正常的方法打網址和獲取XMl。所以我必須解析上面的XMl。 – 2011-12-24 09:10:52