2

I want to serialize an object using XmlSerializer and want it to use the order defined in a XSD.

For example I have an XSD with element like this:

...
<xs:element name="Screen" minOccurs="1" maxOccurs="1">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Yaxis" minOccurs="1" maxOccurs="1" type="ab:AxisType"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
...

<xs:complexType name="AxisType">
  <xs:sequence>
    <xs:group ref="ab:IntervalSegmentGroup" minOccurs="1" maxOccurs="unbounded"/>
    <xs:element name="LastPoint" minOccurs="1" maxOccurs="1" type="ab:PointType" />
  </xs:sequence>
</xs:complexType>

<xs:group name="IntervalSegmentGroup">
  <xs:sequence>
    <xs:element name="Point" minOccurs="1" maxOccurs="1" type="ab:PointType"/>
    <xs:element name="Interpolation" minOccurs="1" maxOccurs="1" type="xs:string"/>
  </xs:sequence>
</xs:group>

<xs:complexType name="PointType">
  <xs:sequence>
    <xs:element name="Relative" type="xs:decimal" minOccurs="1" maxOccurs="1"/>
    <xs:element name="Absolute" type="xs:decimal" minOccurs="1" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>
...

I have then used XSD.exe to create stub-classes for this. Then I create objects from these claseses and populate them with data. And then I serialize them with an XMLSerializer.

The result I WANT to get is something like this:

...
<Yaxis>
  <Point>
    <Relative>0</Relative>
    <Absolute>0</Absolute>
  </Point>
  <Interpolation>linear</Interpolation>
  <Point>
    <Relative>0.456</Relative>
    <Absolute>100</Absolute>
  </Point>
  <Interpolation>linear</Interpolation>
  <Point>
    <Relative>0.645</Relative>
    <Absolute>342</Absolute>
  </Point>
  <Interpolation>linear</Interpolation>
  <LastPoint>
    <Relative>1</Relative>
    <Absolute>3920</Absolute>
  </LastPoint>
</Yaxis>
...

But what i get is:

...
<Yaxis>
  <Point>
    <Relative>0</Relative>
    <Absolute>0</Absolute>
  </Point>
  <Point>
    <Relative>0.456</Relative>
    <Absolute>100</Absolute>
  </Point>
  <Point>
    <Relative>0.645</Relative>
    <Absolute>342</Absolute>
  </Point>
  <Interpolation>linear</Interpolation>
  <Interpolation>linear</Interpolation>
  <Interpolation>linear</Interpolation>
  <LastPoint>
    <Relative>1</Relative>
    <Absolute>3920</Absolute>
  </LastPoint>
</Yaxis>
...

When I try to validate the XML against the XSD it will fail, as it doesn't confirm to the order Point, Interpolation, Point...

What am I doing wrong? Can I control this in some kind of way?

4

1 回答 1

0

尝试在生成类时为 XSD.exe 指定 Order 参数。

XSD.exe。

/order 在所有粒子成员上生成明确的顺序标识符。

可能这可以解决您的问题。

于 2012-04-17T11:40:21.283 回答