Why is a field in an auto-generated class serialized into an element when defined as an attribute in the XSD?

我怕爱的太早我们不能终老 提交于 2019-12-24 06:37:22

问题


I'm trying to expose a fairly complex object as XML through a REST API (using WCF).

However, the object is defined by an XSD file so I've generated classes using the xsd.exe tool. The problem is that it seems that when my object is serialized to XML, an attribute (defined in the xsd) is serialized into an element. And I don't get why. Currently, I'm assuming that my xsd somehow allows that, but I can't tell why. I don't do any custom serialization, I let the framework handle it.

Can someone explain why this is happening and how do I control the behavior?

Here the part from the xsd containing the element and attribute. Edit: the attribute in question is version

<xs:schema xmlns:b="http://some.namespace.com/" xmlns="http://someothernamespace.com/" elementFormDefault="qualified" targetNamespace="http://someothernamespace.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="JobPositionPosting.xsd" />
  <xs:element name="Envelope">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Sender" />
        <xs:element minOccurs="0" ref="TransactInfo" />
        <xs:element maxOccurs="unbounded" ref="Packet" />
      </xs:sequence>
      <xs:attribute fixed="0.52" name="version" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>

And here's the generated code.

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

    /// <remarks/>
    public Sender Sender;

    /// <remarks/>
    public TransactInfo TransactInfo;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Packet")]
    public Packet[] Packet;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute("version")]
    public string version;

    public Envelope()
    {
        this.version = "0.52";
    }
}

And here's the xml returned from the REST service, i.e. the serialized object.

<!-- (the rest o the xml is left out on purpose) -->
<Envelope>
    <senderField i:nil="true"/>
    <transactInfoField i:nil="true"/>
    <versionField>0.52</versionField>
</Envelope>

Thanks!


回答1:


xsd.exe produces classes to be used by System.Xml.Serialization.XmlSerializer, while your REST-service is using some variant of System.Runtime.Serialization.DataContractSerializer.

For DataContractSerializer, you should use svcutil.exe instead.

svcutil.exe /help
svcutil.exe schema.xsd /dconly


来源:https://stackoverflow.com/questions/11449564/why-is-a-field-in-an-auto-generated-class-serialized-into-an-element-when-define

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!