2

I have an xml file I'm trying to deserialize using the .Net XmlSerializer class. I'm having trouble coming up with a C# class that represents the xml format. My main problem is dealing with one element that's used to represent a generic array. The sub-elements of the array are not always the same. I'm also having an issue with arrays of arrays, which I've read isn't natively supported.

Example:

<root>
    <parent_1>
        <sub_element0 value="0"/>
        <sub_element1>
            <array idx="0" value="0"/>
            <array idx="1" value="0"/>
        </sub_element1>
    </parent_1>
    <parent_2>
        <array idx="0">
            <array idx="0">
                <sub_element2 value="0"/>
            </array>
            <array idx="1">
                <sub_element2 value="0"/>
            </array>
        </array>
        <array idx="1">
            <array idx="0">
                <sub_element2 value="0"/>
            </array>
            <array idx="1">
                <sub_element2 value="0"/>
            </array>
        </array>
    </parent_2>
</root>

As you can see the array element is used with no sub-elements and with sub-elements (including itself). I can't just create a class named 'array,' so how do I handle this?

Any help is appreciated.

Tony
  • 21
  • 2

2 Answers2

2

Run xsd.exe on the XML in question to create a XSD. Then run xsd.exe /c on the generated XSD to create serialization/deserialization classes. You'll note that it doesn't create a class named array, but rather array1 and then applies an XmlElementAttribute to it with the XML name of "array".

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • When I run xsd.exe on the XML file I get the following error: - Cannot add constraint to DataTable 'array' which is a child table in two nested relations – Tony Dec 09 '10 at 14:19
  • Huh - I didn't get that with the .NET 4 (VS 2010) version of xsd.exe. Which are you using? – Jesse C. Slicer Dec 09 '10 at 14:40
  • I should add that the above XML is just a small snippet of the entire XML file I'm using. I ran the xsd program on the example above and it worked fine, so my issue with running this program isn't in the above example. If I knew what that error message meant then I cold probably figure out what's causing the problem. – Tony Dec 09 '10 at 15:42
  • I should apologize at this point as that is beyond my knowledge in this area. I'll keep a lookout if something gives a clue. – Jesse C. Slicer Dec 09 '10 at 17:44
1

xsd.exe does not support XML instances where some subnode has the same name as a node higher up. In this case you have 'array' with a child with the same name (differentiated with the property 'idx'). This is a known issue and as it's been known for years, I would not hold your breath on it being sorted.

I've hit the same thing today and I'm having a poke around for a solution. If I find this page again, I'll update.

UPDATE: My solution was to write my own deserialer classes, the XML I was receiving was not great having repeated elements within elements &c. I knocked up some classes which recursed through the XML pulling out the elements - very quick and easy and nice.

However, if that is not good for you, try this:

  1. Download trang.jar (it is a Java XSD from XML inferrer) (google for location)
  2. Run it on your XML file like this 'java -jar trang.jar
  3. That gives you an XSD file
  4. Slap that in to XSD.exe with 'xsd /classes'

You'll have something which doesn't look nice, but it will work.

kully
  • 71
  • 2