Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
HTML
<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:
HTML
<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:
HTML
<rootTag>
  <point name="I define a point" x="3" y="3" otherAttributes="">
    <description>some text here</description>
  </point>
</rootTag>
Posted
Updated 10-Feb-12 3:46am
v6
Comments
Charlie Matherne 10-Feb-12 15:55pm    
Solution 2 lead me to my solution. I couldn't use the code created by xsd.exe as is. It would have caused too many issues (re-working existing code, re-testing, etc.). However, the solution brought to my attention XmlSerialization. So, I've started going through my existing class structure and implementing the correct XmlSerialization attributes for saving my information to XML.

BIG THANK YOU goes out to Espen for all of his effort!!

Have a look at xsd.exe[^]

The XML Schema Definition tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

Update
Have a look at Extreme XML:XML Serialization in the .NET Framework[^]

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Christian Graus 10-Feb-12 10:25am    
He's going in the other direction. He has an XSD and wants his XML to conform to it when he adds elements from code.
Espen Harlinn 10-Feb-12 10:28am    
use xsd.exe to generate the required code based on the xsd - OP can then use the generated code together with the XmlSerializer.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(VS.71).aspx
Charlie Matherne 10-Feb-12 10:51am    
Problem is caused by "point". I have two definitions of "point" so it won't create both tables in the DataSet.
Espen Harlinn 10-Feb-12 10:52am    
Have you tried not using the dataset option? or renaming one of them?
Charlie Matherne 10-Feb-12 10:58am    
What's another option? other than dataset...
XMLDocument uses the DOM, so you have methods to insert nodes where-ever you like.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.prependchild.aspx[^]

is probably what you want.
 
Share this answer
 
Comments
Charlie Matherne 10-Feb-12 10:53am    
This solution suggest that I "ask" the XSD for the location/order of the element I'm trying to place...correct?
Christian Graus 10-Feb-12 10:56am    
I'm assuming you know the order, yes. I don't know of any way to tell the class your XSD and have it work out where to put things.
Wonde Tadesse 10-Feb-12 11:17am    
5+
Charlie Matherne 10-Feb-12 11:38am    
Thanks! I was afraid of that.
thatraja 11-Feb-12 9:56am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900