簡體   English   中英

如何定義和序列化C#對象以生成特定的xml?

[英]How to define and serialize c# object(s) to generate specific xml?

我想編寫C#類,以便在將它們序列化為XML時,應生成以下XML模式。

    <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:web="http://webservice.api.cabaret.com/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soapenv:Header/>
   <soapenv:Body>
      <web:callArgs>
         <name></name>
         <args>
            <entries>
               <name>locator.content</name>
               <value xsi:type="xs:string">AA==</value>
            </entries>
            <entries>
               <name>locator.name</name>
               <value xsi:type="xs:string">Reha0850.pdf</value>
            </entries>
         </args>
      </web:callArgs>
   </soapenv:Body>
</soapenv:Envelope>

請提供幫助,我該如何編寫c#類並將其序列化以生成上述XML模式。

通常,如果要自動從XML創建c#類,可以按照“ 從XML生成C#類”或“ 將XML字符串轉換為對象”中的說明進行操作 ,然后使用XmlSerializer

根據提供的XML並按照第一個答案的說明,我在命令提示符下使用xsd.exe創建了類:

prompt> rem Create XSD files
prompt> xsd SoapEnvelope.xml
prompt> rem convert to c# classes
prompt> rem root class is Envelope, in namespace Question35933150 
prompt> xsd SoapEnvelope.xsd SoapEnvelope_app1.xsd /classes /namespace:Question35933150 /e:Envelope

然后,我將包含的類添加到Visual Studio中並進行構建-並發現了一個問題。 xsd.exe推斷value屬性應該是string類型,而不是可能的string類型的多態類型。 該屬性必須是多態的,以便序列化添加xsi:type="xs:string"屬性。 請參見Xsi:type屬性綁定支持使用屬性控制XML序列化:序列化派生類

因此,我不得不通過將value屬性更改為object類型並添加[XmlInclude(typeof(string))]來手動修復生成的類:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://webservice.api.cabaret.com/")]
[XmlInclude(typeof(string))] // Manually added
public partial class callArgsEntries
{
    private string nameField;

    private object valueField; // Manually changed to object

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

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object value // Manually changed to object
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}

我保留的其余自動生成的類是:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{

    private string headerField;

    private EnvelopeBody[] bodyField;

    /// <remarks/>
    public string Header
    {
        get
        {
            return this.headerField;
        }
        set
        {
            this.headerField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Body")]
    public EnvelopeBody[] Body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{

    private callArgs callArgsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://webservice.api.cabaret.com/")]
    public callArgs callArgs
    {
        get
        {
            return this.callArgsField;
        }
        set
        {
            this.callArgsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://webservice.api.cabaret.com/")]
public partial class callArgs
{

    private string nameField;

    private callArgsEntries[] argsField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("entries", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public callArgsEntries[] args
    {
        get
        {
            return this.argsField;
        }
        set
        {
            this.argsField = value;
        }
    }
}

反序列化XML並使用XmlSerializer使用這些類重新序列化,將產生以下XML,該XML具有您所需的架構:

<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Header />
    <Body>
        <callArgs xmlns="http://webservice.api.cabaret.com/">
            <name xmlns="" />
            <args xmlns="">
                <entries>
                    <name>locator.content</name>
                    <value xsi:type="xsd:string">AA==</value>
                </entries>
                <entries>
                    <name>locator.name</name>
                    <value xsi:type="xsd:string">Reha0850.pdf</value>
                </entries>
            </args>
        </callArgs>
    </Body>
</Envelope>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM