How to parse XML (DataSet) in Windows Phone 7

2019-03-06 02:22发布

I have a XML Like Below, But i am not able to parse it. Please help me to parse the below 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>

Here i want the results of the Table (i.e a,b ) tag. I tried using Linq but i am not able parse it. i tried the code some thing like this:

//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").

But this code is returning null.

EDIT: I used below code: String response = 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();

So now resultElement1 i got XML Like Below:

<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>

So Now how can i find the values of tag and tag ?

Thanks in advance.

2条回答
甜甜的少女心
2楼-- · 2019-03-06 02:34

The element GetResult is also in the http://tempuri.org namespace, so that is why you select clause is not finding anything.

Anyhow, I would simplify the query to the following:

public static String myNamespace = "http://tempuri.org/";
XDocument reader = XDocument.Parse(response);
XElement resultElement = reader.Descendants(XName.Get("GetResult", myNamespace)).Single();
查看更多
男人必须洒脱
3楼-- · 2019-03-06 02:36

What is blocking you?

You can try this:

        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);
        }
查看更多
登录 后发表回答