views:

21

answers:

1

I have a requirement to generate XML, validate against an XSD and (if valid), send the XML to a third party.

My problem is that generating the nodes of the XML file in the correct order (as the XSD defines it) is very difficult with my current scenario. Writing the code to manually add the nodes in the correct order would mean a lot of code as well as needing an update if the XSD changes (which it does now and then)

The XSD is fairly complex, with many element refs and groups within sequences and complexTypes.

Unfortunately, I can't attach an example since the XSD (and XML) is regarded as "company secrets".

What I would like to know is if there is a way to sort the nodes in the XML according to the definitions in the XSD?

Anyone ever heard of or seen such a function in the .Net world or otherwise? Anyone have any ideas where to start unraveling this problem?

A: 

Without knowing more it's a little hard to say, but here's how I've addressed similar problems in the past.

Run XSD.EXE from the console, point it at your XSD and use the classes switch (you'll need to look at the documentation, but its not that hard). This will generate an XmlSerializable Class which matches your XSD.

Then you'll write something to convert from your current class to the XSD Class, then serialize out to XML.

AllenG
That's a fascinating approach and I wish I'd known about it when I started this project :) The problem, however, is that I currently have all the code to generate the xml as an XDocument. I now "only" need to sort this XML to comply with the XSD definitions
peter3