c# - 将 soap XML 解析为 C# 类

标签 c# xml linq soap

我正在尝试将 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 非常相似的帖子发布,但我找不到解决方法。

提前致谢! :)

最佳答案

尝试下面的代码。我从第一个标签上取下了 SOAP 。

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;}

        }




}

关于c# - 将 soap XML 解析为 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37949435/

相关文章:

c# - yield return 和 LINQ Select 的不同结果

python - 将 XML 数据解析为 pandas 多索引数据帧

c# - 从 DataTable 创建 XML

c# - 尝试使用 LINQ 过滤集合,其中集合有一个集合,该集合也有一个具有可为空属性的集合

c# - Sum() 在 Entity Framework 查询中返回 null

c# - 在 wcf 的 datacontract 类中公开方法

c# - 一个类怎么可能没有构造函数呢?

c# - 如何计算数据表状态之间的天数

javascript - 如何从 JavaScript 读取 #text 的值

jQuery 解析 XML 制作幻灯片