Windows phone 7 解析soapxml
我有这样一个XML:Windows phone 7 解析soapxml,windows-phone-7,xml-serialization,Windows Phone 7,Xml Serialization,我有这样一个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/XMLSc
<?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>
但我无法将其绑定到列表框。我有一个列表框,如下所示:
<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>
在列表框中,我添加了一个文本块,重复两次。但是动态文本块不与数据绑定。这两个动态值表示为字段,而不是属性。由于只能绑定到XAML中的属性,因此需要使用两个属性创建一个强类型数据结构,并在其中选择值 比如:
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,
});
感谢克劳斯·乔根森的回复。这个解决方案对我有效。克劳斯,还有一个问题。在我的XML中,有时缺少名为tag2的标记。在这里,我想像上面一样解析它,如果特定的标记丢失,那么该对象的标记有时应该为null。
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,
});