将soap XML解析为C#类 [英] Parse a soap XML to a C# class

查看:55
本文介绍了将soap XML解析为C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to parse a SOAP message into a specific Class, but I'm having trouble.

这是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个属性:

I have a simple class with 3 attributes:

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

    public int TimeoutSeconds { get; set; }

    public string ErrorCode { get; set; }
}

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

I'm trying to Use Linq to evaluate the Soap XML and parse it into a an object of the SoapResponse Class. So far I have the next code:

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

我知道这与该非常相似使用LINQ to XML解析SOAP消息,但是我找不到解决它的方法.

I know that this is a very similar post to this Using LINQ to XML to Parse a SOAP message post, but I can't find a way to work it around.

提前谢谢!:)

推荐答案

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

Try code below. I removed soap from first tag.

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

        }




}

这篇关于将soap XML解析为C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆