aatjan
asked on
Write a structure to an xml file: "XML document must have a top level element" error
Hi, i try to write an xml file. When trying to read the file the following error is shown in the browser:-
XML document must have a top level element"
The coding i'm using is
Dim xmlFilename As String = "myXmlDocument.xml"
' Create FileStream
Dim fsWriteXml As New System.IO.FileStream(xmlFi lename, System.IO.FileMode.Create)
' Create an XmlTextWriter to write the file.
Dim xmlWriter As New System.Xml.XmlTextWriter(f sWriteXml, System.Text.Encoding.Unico de)
' Use WriteXml to write the document.
xmlWriter.WriteProcessingI nstruction ("xml", "version='1.0' encoding='ISO-8859-1'") ' eerste regel geschreven
' write the root
xmlWriter.WriteStartElemen t("aaa")
xmlWriter.WriteElementStri ng("date", Nothing, "2006-11-03 12:17:09")
For Each Datarow In TableSelected.Rows
xmlWriter.WriteStartElemen t("client" )
xmlWriter.WriteElementStri ng("id", Nothing, "2006-11-03 12:17:09")
For Each Datarow2 In Table2Selected.Rows
Dim internnr As Integer =Datarow2.Item(1)
xmlWriter.WriteElementStri ng("client nr", Nothing, internnr)
xmlWriter.WriteStartElemen t("profiel ")
For Each Datarow2 In Table3tSelected.Rows
xmlWriter.WriteStartElemen t("profiel _line")
xmlWriter.WriteElementStri ng("id", Nothing, "0000011218")
. ..... more elements .....
xmlWriter.WriteEndElement( ) ' einde profiel_line
Next
xmlWriter.WriteEndElement( ) ' end profiel
xmlWriter.WriteEndElement( ) ' end client ...
Next
xmlWriter.WriteEndElement( ) ' end farmdossier
fsWriteXml.Flush()
fsWriteXml.Close()
What am i doing wrong. BTW: i'm not an XML expert
XML document must have a top level element"
The coding i'm using is
Dim xmlFilename As String = "myXmlDocument.xml"
' Create FileStream
Dim fsWriteXml As New System.IO.FileStream(xmlFi
' Create an XmlTextWriter to write the file.
Dim xmlWriter As New System.Xml.XmlTextWriter(f
' Use WriteXml to write the document.
xmlWriter.WriteProcessingI
' write the root
xmlWriter.WriteStartElemen
xmlWriter.WriteElementStri
For Each Datarow In TableSelected.Rows
xmlWriter.WriteStartElemen
xmlWriter.WriteElementStri
For Each Datarow2 In Table2Selected.Rows
Dim internnr As Integer =Datarow2.Item(1)
xmlWriter.WriteElementStri
xmlWriter.WriteStartElemen
For Each Datarow2 In Table3tSelected.Rows
xmlWriter.WriteStartElemen
xmlWriter.WriteElementStri
. ..... more elements .....
xmlWriter.WriteEndElement(
Next
xmlWriter.WriteEndElement(
xmlWriter.WriteEndElement(
Next
xmlWriter.WriteEndElement(
fsWriteXml.Flush()
fsWriteXml.Close()
What am i doing wrong. BTW: i'm not an XML expert
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi G#,
The Xmlwriter.flush did it. Now i'm working on
the encoding issue,
indentation issue. Currently it produces a file which contains one line ...
Any help would be appreciated ...
Aat Jan
The Xmlwriter.flush did it. Now i'm working on
the encoding issue,
indentation issue. Currently it produces a file which contains one line ...
Any help would be appreciated ...
Aat Jan
ASKER
G#,
Herewith an example of the fle which is produced:-
<?xml version='1.0' encoding='ISO-8859-1'?><fa rmdossier> <peildatum >2006-11-0 3 12:17:09</peildatum><clien t><id>2006 -11-03 12:17:09</id><clientnr>329 1</clientn r><profiel ><profiel_ line><id>0 000011218< /id><atk_i d>14277514 </atk_id>< artikelnaa m>ranitidi ne</artike lnaam><geb rtekst>2x daags 1 tablet</gebrtekst><startda t>2006-11- 03 12:17:09</startdat><enddat >2006-11-0 3 12:17:09</enddat><apotheek _agb>8066< /apotheek_ agb><voors chrijver_a gb>22458</ voorschrij ver_agb><v oorschrijv er>Arts/vo orschrijve r CL</voorschrijver><gebrcod e /><gebrtekst>2x daags 1 tablet</gebrtekst><hoeveel heid>100</ hoeveelhei d><eenheid >ST</eenhe id><hoevee lheid_tego ed>200</ho eveelheid_ tegoed><ho eveelheid_ herhalen_p erkeer>100 </hoeveelh eid_herhal en_perkeer ><einddatu m_herhalen /><soort_medicatie>C</soor t_medicati e></profie l_line></p rofiel></c lient></fa rmdossier>
With regards, aat jan
Herewith an example of the fle which is produced:-
<?xml version='1.0' encoding='ISO-8859-1'?><fa
With regards, aat jan
ASKER
Hi G#,
Last issue is now solved.
Dim xmlWriter As New System.Xml.XmlTextWriter(f sWriteXml, System.Text.Encoding.ASCII ) did the work!
Many thanks for your quick intervention!!!
Aat Jan
Last issue is now solved.
Dim xmlWriter As New System.Xml.XmlTextWriter(f
Many thanks for your quick intervention!!!
Aat Jan
Your welcome Aat Jan :)
If you're still concerned amount the formatting (all on one line). add the following lines of code just before the WriteProcessingInstruction code:
xmlWriter.Formatting = Xml.Formatting.Indented
xmlWriter.IndentChar = " "
xmlWriter.Indentation = 4
This will format the XML document correctly (with an indent of 4 <Space> characters).
If you want to indent with other characters (a Tab for example), you might use the following:
xmlWriter.Formatting = Xml.Formatting.Indented
xmlWriter.IndentChar = vbTab
xmlWriter.Indentation = 1
Cheers,
G#
If you're still concerned amount the formatting (all on one line). add the following lines of code just before the WriteProcessingInstruction
xmlWriter.Formatting = Xml.Formatting.Indented
xmlWriter.IndentChar = " "
xmlWriter.Indentation = 4
This will format the XML document correctly (with an indent of 4 <Space> characters).
If you want to indent with other characters (a Tab for example), you might use the following:
xmlWriter.Formatting = Xml.Formatting.Indented
xmlWriter.IndentChar = vbTab
xmlWriter.Indentation = 1
Cheers,
G#
ASKER
Thanks for your attention!! I'll gonna try this.
Some info:-
- asp.net 1.1
- vs studio 2003
The encoding instruction is necessary. Not because of the processing system has to use it, but because the produced XML file needs to be exactly produced as another system does: this piece of coding is part of a test program which is in fact a mock up of several other systems ...
Y'll get shortly my comments ...
Cheers,
Aat Jan