Search code examples
c#windows-phone-8datasetasmxxelement

How to parse an ArrayOfXElement in Windows Phone?


I'm trying to connect to an ASMX web service and the code generated in a Windows Phone Project is different than in Windows Forms. In Windows Forms, the methods of this web service returns a DataSet, so I go through all rows in the existing tables of this object:

MyObject myObject = new MyObject();
DataSet dataSet = soapClient.SomeMethod();
foreach (DataTable table in dataSet.Tables)
{
    foreach (DataRow row in table.Rows)
    {
        myObject.SomeProperty = row["SomeProperty"];
        myObject.SomeOtherProperty = row["SomeOtherProperty"];
    }
}

But in Windows Phone it generates an async version of this method which I subscribe to an event that fires when the request is completed. And the event args brings me a ArrayOfXElement, which seems to be a Windows Phone version of the DataSet object. So how do I parse this?

Before you mark this question as duplicated, know that the other answers available in this site is not working.


Solution

  • Alright, I managed to get this working. This web service method always return two XElements, which the first one is just a header, declaring what's coming next. So I ignore the first XElement, and inside the NewDataSet element there's a Table element, which has the content I want.

    bool first = true;
    foreach (XElement xEl in e.Result.Nodes)
    {
        if (first)
        { 
            // Ignore the first XElement
            first = false; continue;
        }
    
        var dataSetElement = xEl.Element("NewDataSet");
        var tableElement = dataSetElement.Element("Table");
    
        // And here's the useful data
        tableElement.Element("Property1").Value;
        tableElement.Element("Property2").Value;
    }
    

    Notice that this only gets the first table row. If there's more than one table row, you'll find the others rows with the msdata:rowOrder attribute.