解析 Soap XML

发布于 2024-12-26 04:43:30 字数 2353 浏览 2 评论 0原文

我有一个像这样的 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>

在列表框中我添加了一个 TextBlock,它重复两次。但动态文本块不与数据绑定。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑凤梨 2025-01-02 04:43:30

这两个动态值表示为字段,而不是属性。由于您只能绑定到 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,
             });

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,
             });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文