XSD Class generators: keeping track of element ord

2019-08-04 00:54发布

问题:

I got the following complex type within my XSD schema

<xs:complexType name="structure" mixed="true">
  <xs:choice maxOccurs="unbounded">
    <xs:element type="b" name="b" />
    <xs:element type="a" name="a" />
  </xs:choice>
</xs:complexType>

which allows me to state XML definitions like this:

<structure>
    Hello <b>World</b> 
    Hello 2 <b>World 2</b> 
    <a>Hello3</a> <b>World3</b>
</structure>

Now I tried to generate XSD classes out of my schema, I tried both XSD.exe as well as XSD2Code. They both generate something like

class structure {
    List<a> a;
    List<b> b;
    List<string> text;
}

My problem is, that I need to keep track in which order those elements where defined within the XML content of structure. Refering to the above example, I would like to know that the inner text "Hello" comes right before the first occurance of the b-element.

As this would obviously require a more specialized generator strategy, maybe I'm expecting too much, but: is there any XSD generator that can handle the object order or do I have to write my own classes?

Thank you in advance

回答1:

I have never seen an XSD to code binding tool which would do what you need here, for sure not on the .NET platform - which you seem to imply as the target. This is one of those cases where roundtrip an XML is not possible, without loss of fidelity (deserialize, serialize then compare, it fails). Just for completeness, the /order option wouldn't work with xsd.exe, simply because in terms of the XSD you defined, there's no order really. It is, also, a limitation of what XSD can describe, which inevitably is reflected in tool implementations.