【问题标题】:parsing SOAP response in C#...not understanding the XML namespaces在 C# 中解析 SOAP 响应...不理解 XML 命名空间
【发布时间】:2013-05-03 05:11:28
【问题描述】:

请原谅,我是 SOAP 和 C# 的新手。我似乎无法弄清楚如何正确设置命名空间以在 SOAP 响应中查找节点。

以下是 Web 服务查询返回空时的响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:VXWSResponse xmlns:ns="vx.sx">
            <ns:List ns:id="result" />
        </ns:VXWSResponse>
    </soapenv:Body>
</soapenv:Envelope>

这是返回数据时的响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:VXWSResponse xmlns:ns="vx.sx">
            <ns:List ns:id="result">
                <ns:Badge>USER DATA</ns:Badge>
            </ns:List>
        </ns:VXWSResponse>
    </soapenv:Body>
</soapenv:Envelope>

我只需要知道标签是否存在。

这是我目前所拥有的。

XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("ns", "vx.sx");
manager.AddNamespace("id", "result");
xmlNode badge = xml.SelectSingleNode("//id:Badge", manager);
XmlNode result = xml.SelectSingleNode("//ns:result", manager);

两个节点都返回 null。我查看了该站点上的大量其他文章,但没有看到如何正确处理响应 XML 中的命名空间。

感谢任何帮助!

【问题讨论】:

  • 你真的需要手动解析 SOAP 吗?你想达到什么目的?
  • 我无法控制 Web 服务,这需要在 CE 5 / .NET 2 上运行,所以我不确定是否可以使用服务参考。我只需要测试 标签是否存在。

标签: c# xml xml-parsing


【解决方案1】:

id 是列表节点的属性,而不是命名空间。

我编辑了我的答案以检查 Badge 元素,因为这就是您想要查找的全部内容。

        XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
        manager.AddNamespace("ns", "vx.sx");

        XmlNode badge = xmlDoc.SelectSingleNode("//ns:Badge", manager);

        if (badge == null)
        {
          // no badge element
        }
        else
        {
            // badge element present
        }

【讨论】:

  • 做到了。知道这将是一件简单的事情。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 2010-11-08
相关资源
最近更新 更多