gpt4 book ai didi

c# - 序列化对象,保留 xs :group 的顺序

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:58 24 4
gpt4 key购买 nike

我想使用 XmlSerializer 序列化一个对象,并希望它使用 XSD 中定义的顺序。

例如,我有一个包含如下元素的 XSD:

...
<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>
...

然后我使用 XSD.exe 为此创建 stub 类。然后我从这些类中创建对象并用数据填充它们。然后我用 XMLSerializer 序列化它们。

我想要得到的结果是这样的:

...
<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>
...

但我得到的是:

...
<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>
...

当我尝试根据 XSD 验证 XML 时,它将失败,因为它没有确认顺序点、插值、点...

我做错了什么?我能以某种方式控制它吗?

最佳答案

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

XSD.exe。

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

这可能会解决您的问题。

关于c# - 序列化对象,保留 xs :group 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10186471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com