使用 C# 和 XDocument/XElement 解析 Soap 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/779976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:52:01  来源:igfitidea点击:

Using C# and XDocument/XElement to parse a Soap Response

c#xmlsoapparsing

提问by strickland

Here is a sample soap response from my SuperDuperService:

这是来自我的 SuperDuperService 的示例肥皂响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <MyResponse xmlns="http://mycrazyservice.com/SuperDuperService">
            <Result>32347</Result> 
        </MyResponse>
    </soap:Body>
</soap:Envelope>

For some reason when I try to grab the Descendant or Element of "Result" I get null. Does it have something to do with the Namespace? Can someone provide a solution to retrieve Result from this?

出于某种原因,当我尝试获取“结果”的后代或元素时,我得到了空值。它与命名空间有关吗?有人可以提供解决方案来从中检索结果吗?

采纳答案by Justin Niessner

You might want to try something like this:

你可能想尝试这样的事情:

string myNamespace= "http://mycrazyservice.com/SuperDuperService";

var results = from result in yourXml.Descendants(XName.Get("MyResponse", myNamespace))
              select result.Element("Result").value

Don't have VS on this laptop so I can't double check my code, but it should point you in the right direction using LINQ to SQL.

这台笔记本电脑上没有 VS,所以我无法仔细检查我的代码,但它应该为您指明使用 LINQ to SQL 的正确方向。

回答by Paw Baltzersen

Maybe like this:

也许是这样的:

IEnumerable<XElement> list = doc.Document.Descendants("Result");
if (list.Count() > 0)
{
    // do stuff
}

回答by Heebr

You're searching in the right direction, it definitely has to do with the namespace.

您正在朝着正确的方向进行搜索,这肯定与命名空间有关。

The code below returns the first element found for the combination of namespace and element name.

下面的代码返回为命名空间和元素名称的组合找到的第一个元素。

XDocument doc = XDocument.Load(@"c:\temp\file.xml");
XNamespace ns = @"http://mycrazyservice.com/SuperDuperService";
XElement el = doc.Elements().DescendantsAndSelf().FirstOrDefault( e => e.Name == ns + "Result");

回答by azpc

to extend Justin's answer with tested code with a return that excpects a boolean and that the response and result start with the method name (BTW - a surprise is even thought the XML element does not show the NS it requires it when parsing):

使用经过测试的代码扩展 Justin 的答案,并返回一个布尔值,并且响应和结果以方法名称开头(顺便说一句 - 甚至认为 XML 元素在解析时没有显示它需要的 NS 令人惊讶):

    private string ParseXml(string sXml, string sNs, string sMethod, out bool br)
    {
        br = false;
        string sr = "";
        try
        {
            XDocument xd = XDocument.Parse(sXml);

            if (xd.Root != null)
            {
                XNamespace xmlns = sNs;
                var results = from result in xd.Descendants(xmlns + sMethod + "Response")
                              let xElement = result.Element(xmlns + sMethod + "Result")
                              where xElement != null
                              select xElement.Value;
                foreach (var item in results)
                    sr = item;
                br = (sr.Equals("true"));
                return sr;
            }
            return "Invalid XML " + Environment.NewLine + sXml;
        }
        catch (Exception ex)
        {
            return "Invalid XML " + Environment.NewLine + ex.Message + Environment.NewLine + sXml;
        }
    }