2011-12-29 50 views
0

我有一個像下面的XML,但我無法解析它。請幫我解析下面的XML。如何在Windows Phone 7中解析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 xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> 
<NewDataSet xmlns=""> 
<Table diffgr:id="Table1" msdata:rowOrder="0"> 
<a>hi1</a> 
<b>hi2</b> 
</Table> 
<Table diffgr:id="Table2" msdata:rowOrder="1"> 
<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。

編輯: 我用下面的代碼: 字符串響應= e.response;

public static String myNamespace = "http://tempuri.org/"; 
XDocument reader = XDocument.Parse(response); 
XElement resultElement = reader.Descendants(XName.Get("GetResult", myNamespace)).Single(); 
XElement resultElement1 = resultElement.Descendants(XName.Get("NewDataSet", "")).Single(); 

所以現在resultElement1我得到了XML象下面這樣:

<NewDataSet xmlns=""> 
    <Table diffgr:id="Table1" msdata:rowOrder="0" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> 
<a>hi1</a> 
<b>hi2</b> 
</Table> 
<Table diffgr:id="Table2" msdata:rowOrder="1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> 
<a>hi3</a> 
<b>hi4</b> 
</Table> 
</NewDataSet> 

所以現在我如何才能找到標籤和標籤的價值?

在此先感謝。

回答

0

元素GetResult也在http://tempuri.org命名空間,所以這就是爲什麼你選擇子句沒有找到任何東西。

無論如何,我會簡化查詢到以下幾點:

public static String myNamespace = "http://tempuri.org/"; 
XDocument reader = XDocument.Parse(response); 
XElement resultElement = reader.Descendants(XName.Get("GetResult", myNamespace)).Single(); 
1

是什麼阻礙了你?

你可以試試這個:

 var resultNewDataSet = XElement.Parse(xmlContent); 

     var result = resultNewDataSet.Descendants("Table") 
      .Select(t => new 
       { 
        aaa = t.Descendants("aaa").First().Value, 
        bbb = t.Descendants("bbb").First().Value 
       }); 

     foreach (var res in result) 
     { 
      System.Diagnostics.Debug.WriteLine("aaa: " + res.aaa + "/bbb: " + res.bbb); 
     } 
+0

感謝您的答覆kookiz。現在我能夠獲得aaa和bbb標籤的值。但在這裏我想將它綁定到ListBox.ItemsSource;我怎樣才能做到這一點。我在ListBox中有如下的文本框,但數據不具有約束力。 「」 – Avinash 2012-01-03 05:34:53