Mahi Kumar
Mahi Kumar

Reputation: 171

Parsing Soap XML

I have a XML like this:

<?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>
        <SampleResponse xmlns="http://tempuri.org/">
            <SampleResult>
                <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">
                            <tag1>tag1 text</tag1>
                            <tag2>tag2 text</tag2>
                        </Table>
                        <Table diffgr:id="Table2" msdata:rowOrder="1">
                            <tag1>tag1 text</tag1>
                            <tag2>tag2 text</tag2>
                        </Table>
                    </NewDataSet>
                </diffgr:diffgram>
            </SampleResult>
        </SampleResponse>
    </soap:Body>
</soap:Envelope>

So i parsed above XML using Below Code:

string XMLresponse = e.response;
var XResult = XElement.Parse(XMLresponse);

var result = XResult.Descendants("Table")
    .Select(t => new
    {
        tag1 = t.Descendants("tag1").First().Value,
        tag2 = t.Descendants("tag2").First().Value,
    });

foreach (var res in result)
{
   string str = res.tag1; // here i am able get the response
}

listbox.ItemsSource = result;

But i am not able bind it to ListBox. i have a ListBox like below:

<ListBox x:Name="listbox">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="Sample" ></TextBlock>
                <TextBlock Text="{Binding tag1}" ></TextBlock>
                <TextBlock Text="{Binding tag2}" ></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In the ListBox i have added a TextBlock, which is repeated twice. But the dynamical text blocks are not binding with data.

Upvotes: 0

Views: 320

Answers (1)

Claus J&#248;rgensen
Claus J&#248;rgensen

Reputation: 26336

The two dynamic values are represented as fields, not properties. And since you can only bind to properties in XAML, you'll need to create a strong-typed data structure with your two properties, and select the values into that.

Something like:

class V 
{ 
    public string tag1 { get; set; }
    public string tag2 { get; set; }
}

var result = XResult.Descendants("Table").Select(t => new V
             {
                 tag1 = t.Descendants("tag1").First().Value,
                 tag2 = t.Descendants("tag2").First().Value,
             });

Upvotes: 1

Related Questions