繁体   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