I working with an XML file that belongs to an InfoPath 2003 Form.
I'm using VS2005, vb.net on a Mobile 6 platform for this app.
Basically, I move the XML file to the mobile device, run the app which uses
the XML file, and then later on move it back to a network server where user can continue
to use it InfoPath or route it to supervisor, etc.
My issue is after the user saves their data on the mobile, the schema is slightly rewritten. Subsequently, when attempting to open it in InfoPath, that program complains about the schema.
Here's my code.
Dim ds As New DataSet
ds.ReadXml("filename.xml")
.
.
. populate some controls on a win mobile form
.
.
. save data from dataset back into the XML file
.
.
ds.AcceptChanges()
ds.WriteXml("filename.xml")
The XML before I touch it is as follows:
<A></A>
<B>
<B1></B1>
<B2></B2>
</B>
<C></C>
After I write it back, it is changed into this. Actually, just after the readxml it changes. I checked it in debug mode, xml visualizer.
<A></A>
<C></C>
<B>
<B1></B1>
<B2></B2>
</B>
Everything is there, just not in the correct order.
My data is actually more complex than this. I am simplifying it here for discussion's sake.
InfoPath no longer is able to read it and re-populate it's form.
How can I get the ds to preserve the correct order of the elements?
OK, even when read in the schema (from .xsd), I can't seem to be able to write it while respecting the schema (at least as far as the order of the elements is concerned).
I tried:
Dim ds As New DataSet
ds.ReadXmlSchema("filename.xsd")
ds.ReadXml("filename.xml")
.
.
. populate some controls on a win mobile form
.
.
. save data from dataset back into the XML file
.
.
ds.AcceptChanges()
ds.WriteXml("filename.xml") << -- not sure here how to make it respect the schema.
I've looked at the xmlwritemode(s) but am not having success.
I'm also looking into nested data relations.
In your senario, it is better to loop through table rows to output xml node manually instead of invoking dataset's writexml method.
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
While I still don't fully understand why a dataset won't preserve the order of the complex elements, I was able to get around this by using the system.xml.xmldocument class instead of a dataset. It's more work but is clean and does perform as expected.
CSpace
Member
216 Points
136 Posts
Preserve Order of Elements in XML file while using Dataset
Mar 26, 2009 04:21 PM|LINK
Hi,
I working with an XML file that belongs to an InfoPath 2003 Form.
I'm using VS2005, vb.net on a Mobile 6 platform for this app.
Basically, I move the XML file to the mobile device, run the app which uses
the XML file, and then later on move it back to a network server where user can continue
to use it InfoPath or route it to supervisor, etc.
My issue is after the user saves their data on the mobile, the schema is slightly rewritten. Subsequently, when attempting to open it in InfoPath, that program complains about the schema.
Here's my code.
Dim ds As New DataSet
ds.ReadXml("filename.xml")
.
.
. populate some controls on a win mobile form
.
.
. save data from dataset back into the XML file
.
.
ds.AcceptChanges()
ds.WriteXml("filename.xml")
The XML before I touch it is as follows:
<A></A>
<B>
<B1></B1>
<B2></B2>
</B>
<C></C>
After I write it back, it is changed into this. Actually, just after the readxml it changes. I checked it in debug mode, xml visualizer.
<A></A>
<C></C>
<B>
<B1></B1>
<B2></B2>
</B>
Everything is there, just not in the correct order.
My data is actually more complex than this. I am simplifying it here for discussion's sake.
InfoPath no longer is able to read it and re-populate it's form.
How can I get the ds to preserve the correct order of the elements?
Thanks in advance.
CSpace
Member
216 Points
136 Posts
Re: Preserve Order of Elements in XML file while using Dataset
Mar 26, 2009 08:17 PM|LINK
OK, even when read in the schema (from .xsd), I can't seem to be able to write it while respecting the schema (at least as far as the order of the elements is concerned).
I tried:
Dim ds As New DataSet
ds.ReadXmlSchema("filename.xsd")
ds.ReadXml("filename.xml")
.
.
. populate some controls on a win mobile form
.
.
. save data from dataset back into the XML file
.
.
ds.AcceptChanges()
ds.WriteXml("filename.xml") << -- not sure here how to make it respect the schema.
I've looked at the xmlwritemode(s) but am not having success.
I'm also looking into nested data relations.
Any ideas?
Samu Zhang -...
All-Star
62163 Points
6101 Posts
Re: Preserve Order of Elements in XML file while using Dataset
Mar 30, 2009 04:41 AM|LINK
Hi CSpace ,
The order of xmlelements can not be preserve unless elements are wraped in xs:sequence indicator. http://www.w3schools.com/schema/schema_complex.asp
In your senario, it is better to loop through table rows to output xml node manually instead of invoking dataset's writexml method.
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
CSpace
Member
216 Points
136 Posts
Re: Preserve Order of Elements in XML file while using Dataset
Mar 30, 2009 02:53 PM|LINK
Samu,
Thanks for your reply.
The schema file does contain the xs:sequence indicators.
Here's an actual sample of the XML XSD file.
<xs:element name="ChildPlacementInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildAppearence" type="xs:string"/>
<xs:element name="ChildAppearenceComments" type="xs:string"/>
<xs:element name="ParentComments" type="xs:string"/>
<xs:element name="ChildBehavior">
<xs:complexType>
<xs:sequence>
<xs:element name="AgeInfant" type="xs:string"/>
<xs:element name="AgeToddler" type="xs:string"/>
<xs:element name="PossibleDevDelayed" type="xs:string"/>
<xs:element name="InteractiveCareTaker" type="xs:string"/>
<xs:element name="ResponsiveToDirection" type="xs:string"/>
<xs:element name="NonResponsiveToDirection" type="xs:string"/>
<xs:element name="ExcessivelyActive" type="xs:string"/>
<xs:element name="Others" type="xs:string"/>
<xs:element name="Age412" type="xs:string"/>
<xs:element name="AgeTeenager" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="IsDayCare">
<xs:complexType>
<xs:sequence>
<xs:element name="DayCare" type="xs:string"/>
<xs:element name="TheraPuticDayCare" type="xs:string"/>
<xs:element name="PreSchool" type="xs:string"/>
<xs:element name="School" type="xs:string"/>
<xs:element name="NA" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ChildBehCount" nillable="true" type="xs:integer"/>
<xs:element name="DayCareCount" nillable="true" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Is that not sufficient to have the dataset write comnmand preserve the order?
Regards
CSpace
Member
216 Points
136 Posts
Re: Preserve Order of Elements in XML file while using Dataset
Mar 31, 2009 07:46 PM|LINK
While I still don't fully understand why a dataset won't preserve the order of the complex elements, I was able to get around this by using the system.xml.xmldocument class instead of a dataset. It's more work but is clean and does perform as expected.
TJinWI
Member
2 Points
1 Post
Re: Preserve Order of Elements in XML file while using Dataset
Nov 30, 2012 06:36 PM|LINK
Having this problem now. Looking for an example, or point to a reference. Thanks.