2011-12-29 102 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