Sort XML file based on XSD format in .Net
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.
Unfortunate开发者_StackOverflow中文版ly, 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?
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.
精彩评论