将SOAP XML解析为C#类

met*_*ogo 1 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>
Run Code Online (Sandbox Code Playgroud)

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

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

    public int TimeoutSeconds { get; set; }

    public string ErrorCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用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
    };
Run Code Online (Sandbox Code Playgroud)

我知道这是一个非常类似的帖子使用LINQ to XML来解析SOAP消息帖子,但我找不到一种方法来解决它.

提前致谢!:)

jdw*_*eng 5

请尝试以下代码.我从第一个标签中删除了肥皂.

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

        }




}
Run Code Online (Sandbox Code Playgroud)