У меня есть такой 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>
<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>
Итак, я проанализировал XML-код, используя приведенный ниже код:
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;
Но я не могу связать его со списком.У меня есть ListBox, как показано ниже:
<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>
В ListBox я добавил TextBlock, который повторяется дважды.Но динамические текстовые блоки не связаны с данными.