【问题标题】:Deserialize part of SOAP response反序列化部分 SOAP 响应
【发布时间】:2019-08-08 15:07:14
【问题描述】:

我对我的肥皂响应的特定部分的反序列化有点迷失了。

回应:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body>
    <ns1:LoginResult xmlns:ns1="http://abc.def.schema">
        <sessionId>123456789</sessionId>
        <sessionTimeout>30</sessionTimeout>
        <organizationName>WebService Test Account XYZ</organizationName>
        <userInfoResult>
            <accessibleOrgs>
                <name>WebService Test Account XYZ</name>
                <description/>
                <prefix>10</prefix>
                <countryCallingCode>+49</countryCallingCode>
                <treeLevel>0</treeLevel>
                <timeZone>
                    <timeZoneId>Europe/Berlin</timeZoneId>
                    <currentUtcOffset>3600000</currentUtcOffset>
                </timeZone>
                <billingCompany>COMPANY 123</billingCompany>
                <language>DE</language>
            </accessibleOrgs>
            <isDemo>true</isDemo>
            <prefixLength>0</prefixLength>
            <alarmNumberLength>0</alarmNumberLength>
            <groupNumberLength>0</groupNumberLength>
            <personNumberLength>0</personNumberLength>
        </userInfoResult>
    </ns1:LoginResult>
</soapenv:Body>

我需要反序列化“LoginResult”部分。我知道反序列化方法,但我正在为 A) 有命名空间和 B) 我只需要 XML 的一个子集而苦苦挣扎。

也许有人可以为我指明正确的方向。

提前谢谢

【问题讨论】:

  • xml 的什么子集?
  • 正如我的帖子中提到的,我需要“LoginResult”部分。但我会检查 EyIM 的建议,会给出反馈:)

标签: c# xml serialization deserialization xml-namespaces


【解决方案1】:

从 LoginResult 类的定义开始。

[XmlRootAttribute(Namespace = "http://abc.def.schema", IsNullable = false, ElementName = "LoginResult")]
public class LoginResult
{
    [XmlElement(Namespace ="")]
    public int sessionId { get; set; }

    [XmlElement(Namespace = "")]
    public string organizationName { get; set; }

     ..... some more properties
}
  1. 使用来自System.Xml.LinqXDocument 类来解析xml。
  2. 找到“LoginResult”元素。
  3. 反序列化为LoginResult类型。

    var xDoc = XDocument.Parse(str);
    var xLoginResult = xDoc.Root.Descendants().FirstOrDefault(d => d.Name.LocalName.Equals("LoginResult"));
    var serializer = new XmlSerializer(typeof(LoginResult));
    using (var reader = xLoginResult.CreateReader())
    {                
        var result = (LoginResult)serializer.Deserialize(reader);
    }
    

【讨论】:

    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多