C# - 反序列化 SOAP 消息

标签 c# soap deserialization

我有这个 SOAP 消息,我需要序列化为 C# 对象:

<SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAPSDK4:GetStoreInformationResponse xmlns:SOAPSDK4="http://www.example.com/message/">
      <StoreInformation>
        <StoreID>99612</StoreID>
        <BusinessDate>2016-01-28</BusinessDate>
        <Address type="Address-US">
           <Street>Via Roma 1</Street>
           <City>Milano</City>
        </Address>
      </StoreInformation>
    </SOAPSDK4:GetStoreInformationResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我发现了很多序列化的例子,但我不能将这些应用到我的案例中。 有人可以帮助我吗?

这是生成的 C# 类:

//------------------------------------------------------------------------------
// <auto-generated>
//     Il codice è stato generato da uno strumento.
//     Versione runtime:4.0.30319.42000
//
//     Le modifiche apportate a questo file possono provocare un comportamento non corretto e andranno perse se
//     il codice viene rigenerato.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// Codice sorgente generato automaticamente da xsd, versione=4.0.30319.33440.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.com/message/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.example.com/message/", IsNullable = false)]
public partial class GetStoreInformationResponse
{

    private GetStoreInformationResponseStoreInformation storeInformationField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public GetStoreInformationResponseStoreInformation StoreInformation
    {
        get
        {
            return this.storeInformationField;
        }
        set
        {
            this.storeInformationField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.com/message/")]
public partial class GetStoreInformationResponseStoreInformation
{

    private string storeIDField;

    private string businessDateField;

    private GetStoreInformationResponseStoreInformationAddress[] addressField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string StoreID
    {
        get
        {
            return this.storeIDField;
        }
        set
        {
            this.storeIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string BusinessDate
    {
        get
        {
            return this.businessDateField;
        }
        set
        {
            this.businessDateField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Address", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public GetStoreInformationResponseStoreInformationAddress[] Address
    {
        get
        {
            return this.addressField;
        }
        set
        {
            this.addressField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.example.com/message/")]
public partial class GetStoreInformationResponseStoreInformationAddress
{

    private string streetField;

    private string cityField;

    private string typeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Street
    {
        get
        {
            return this.streetField;
        }
        set
        {
            this.streetField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string City
    {
        get
        {
            return this.cityField;
        }
        set
        {
            this.cityField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
}

最佳答案

您可以尝试使用xsd.exe工具生成C#类,并使用这段代码:

    /// <summary>
    /// Methode de deserialisation d'objets
    /// </summary>
    /// <param name="xmlObject">Document XML à désérialiser</param>
    /// <returns>Retourne l'objet chargé avec les données du document XML passé en paramètres</returns>
public static ObjectTypeT Deserialize(XmlDocument xmlObject)
{
    if (xmlObject == null)
        throw new NullXmlDocumentException();
    if (xmlObject.DocumentElement == null)
        throw new NullXmlDocumentElementException();

    using (XmlNodeReader reader = new XmlNodeReader(xmlObject.DocumentElement))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ObjectTypeT));
        return (ObjectTypeT)serializer.Deserialize(reader);
    }
}

生成您的对象。您首先必须在 XmlDocument 中加载 Xml。

关于C# - 反序列化 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38347297/

相关文章:

c# - 美国地址验证 (Zip+4)

c# - 在 javascript 中执行 split() 并忽略空白条目的最佳方法是什么?

perl - 如何使用 OTRS 6 中的通用接口(interface)获取用户相关数据?

Android SOAP XML 解析 ListView 错误

serialization - jackson 注释 : Difference Between JsonIgnoreProperties(ignoreUnknown=true) and JsonInclude(Include. NON_EMPTY)

c# - 因非对象字段错误而错误对齐或重叠

c# - 如何在 {x :bind}? 中转换依赖属性

web-services - 休息与 SOAP 。 REST 有更好的性能吗?

JSON Oracle SQL 解析/取消嵌套转义形式的嵌入 JSON 数据

c# - Newtonsoft Json 将值 {null} 转换为类型 'System.Int32' 时出错