【问题标题】:Parse a soap XML to a C# class将肥皂 XML 解析为 C# 类
【发布时间】:2016-06-21 16:06:36
【问题描述】:

我正在尝试将 SOAP 消息解析为特定的类,但遇到了问题。

这是 SOAP 消息:

<?xml version="1.0" encoding="utf-8"?>
<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>
        <LoginResponse
            xmlns="http://schemas.microsoft.com/sharepoint/soap/">
            <LoginResult>
                <CookieName>FedAuth</CookieName>
                <ErrorCode>NoError</ErrorCode>
                <TimeoutSeconds>1800</TimeoutSeconds>
            </LoginResult>
        </LoginResponse>
    </soap:Body>
</soap:Envelope>

我有一个具有 3 个属性的简单类:

public class SoapResponse
{
    public string CookieName { get; set; }

    public int TimeoutSeconds { get; set; }

    public string ErrorCode { get; set; }
}

我正在尝试使用 Linq 评估 Soap XML 并将其解析为 SoapResponse 类的对象。到目前为止,我有下一个代码:

var xml = XDocument.Parse(responseXml);
var soapResponse = from result in xml.Descendants(XName.Get("LoginResult", xmlNamespace))
    let cookieNameElement = result.Element(XName.Get("CookieName", xmlNamespace)) where cookieNameElement != null
    let timoutSecondsElement = result.Element(XName.Get("TimoutSeconds", xmlNamespace)) where timoutSecondsElement != null
    let errorCodeElement = result.Element(XName.Get("ErrorCode", xmlNamespace)) where errorCodeElement != null
    select new SoapResponse
    {
        CookieName = cookieNameElement.Value,
        TimeoutSeconds = Convert.ToInt32(timoutSecondsElement.Value),
        ErrorCode = errorCodeElement.Value
    };

我知道这是与Using LINQ to XML to Parse a SOAP message 帖子非常相似的帖子,但我找不到解决方法。

提前致谢! :)

【问题讨论】:

    标签: c# xml linq soap


    【解决方案1】:

    试试下面的代码。我从第一个标签中删除了肥皂。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    
    namespace ConsoleApplication102
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                string responseXml =
                    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<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>" +
                            "<LoginResponse" +
                                " xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
                                "<LoginResult>" +
                                    "<CookieName>FedAuth</CookieName>" +
                                    "<ErrorCode>NoError</ErrorCode>" +
                                    "<TimeoutSeconds>1800</TimeoutSeconds>" +
                                "</LoginResult>" +
                            "</LoginResponse>" +
                        "</soap:Body>" +
                    "</Envelope>";
    
                XDocument xml = XDocument.Parse(responseXml);
                var soapResponse = xml.Descendants().Where(x => x.Name.LocalName == "LoginResult").Select(x => new SoapResponse()
                {
                    CookieName = (string)x.Element(x.Name.Namespace + "CookieName"),
                    TimeoutSeconds = (int)x.Element(x.Name.Namespace + "TimeoutSeconds"),
                    ErrorCode = (string)x.Element(x.Name.Namespace + "ErrorCode")
                }).FirstOrDefault();
    
            }
    
        }
            public class SoapResponse
            {
                public string CookieName { get; set;}
                public int TimeoutSeconds { get; set;}
                public string ErrorCode { get; set;}
    
            }
    
    
    
    
    }
    

    【讨论】:

    • 使用 System.Xml.Linq 添加;并使用 System.Linq;作为参考,以便使用此答案。
    • 如果您不能简单地“从第一个标签中删除肥皂”怎么办?我正在处理一个非常相似的问题,因为我试图将 XML 从“Body”中拉出来,而在我必须解析 XML 之前,我没有简单地把东西拿出来的奢侈,以“&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"”等开头。
    • @ScottFraley :命名空间用于针对模式执行 xml 验证。针对 xml 的模式检查应由生成 xml 的软件执行。我觉得从供应商那里获取 xml 文件的用户应该进行检查然后回到供应商说他们得到了一个错误的 xml,这很愚蠢。供应商应该发送好的 xml 文件。所以我的代码在解析时只是忽略了命名空间(用于验证模式)。验证应该已经执行。
    • 这里运行良好,谢谢!
    猜你喜欢
    • 2015-09-11
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多