Charlie Matherne
Charlie Matherne

Reputation: 21

Adding new elements to XmlDocument that abide by XSD

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>

Upvotes: 2

Views: 1187

Answers (2)

Charlie Matherne
Charlie Matherne

Reputation: 21

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

Upvotes: 0

Petru Gardea
Petru Gardea

Reputation: 21638

Since you tagged this C#, I assume you're on the .NET platform. The System.Xml.Schema would be your best friend. For a program that uses the above API to generate XML, that also comes with source code you could use to understand how to solve your issue, I would use the XmlSampleGenerator.

Generating a sample XML requires exactly what you need in terms of constraining the XPath the user may enter at a given point in time. I believe you will have to constrain the XPath you allow based on where you are in the editing process, right from the beginning, otherwise, one single mistake could make the whole approach useless.

If you don't constrain from the beginning, it might be impossible to try to re-order based on an XSD (please read this also in SO)...

Upvotes: 1

Related Questions