Currently, I’m adding elements to my XmlDocument using XPath notation for which I’ve written code to that places the element at the proper location in the file. With one exception. I don’t know how to make it pay attention to the sequence rules defined in my XSD file.
Is there a way to add an element to an XmlDocument so that is abides by the sequence define in the XSD that governs my XML file?
For example, my xml document should look like:
<rootTag>
<area name="I define an area">
<description>some text here</description>
<point x="1" y="1" />
<point x="2" y="2" />
<point x="3" y="3" />
</area>
</rootTag>
Yet I get, depending on the order in which the user enters values for the child tags above:
<rootTag>
<area name="I define an area">
<point x="1" y="1" />
<point x="2" y="2" />
<point x="3" y="3" />
<description>some text here</description>
</area>
</rootTag>
To correct the above, I create a DataSet (named tempXmlDataset) from the XSD file. I pass the contents of the XmlDocument into tempXmlDataset and things get re-ordered appropriately.
However, my problem is caused by an option for the first child of the XML document. This option is defined in the XSD to allow for “area”, “line” or “point” objects. “area” and “line” both have “point” elements as children. But child “point” is not the same as “point” object. So, as you might already realize, tempXmlDataset.ReadXmlSchema(…) creates a “point” table which only has x and y in it. This is by definition of the children for “area” and “line”.
So when my code runs tempXmlDataset.ReadXml(…) the attributes for “point” object do not get read in because it sees “point” object as child “point”. Here’s an example of “point” object:
<rootTag>
<point name="I define a point" x="3" y="3" otherAttributes="">
<description>some text here</description>
</point>
</rootTag>
use xsd.exe to generate the required code based on the xsd for classes. Don’t try to create the dataset for this case. You can then use the generated code together with the XmlSerializer to produce the needed xml files.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Also see:
http://msdn.microsoft.com/en-us/library/ms950721.aspx