Link to home
Start Free TrialLog in
Avatar of aatjan
aatjanFlag for Netherlands

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(xmlFilename, System.IO.FileMode.Create)
            ' Create an XmlTextWriter to write the file.
            Dim xmlWriter As New System.Xml.XmlTextWriter(fsWriteXml, System.Text.Encoding.Unicode)
            ' Use WriteXml to write the document.
            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'") ' eerste regel geschreven
            ' write the root
            xmlWriter.WriteStartElement("aaa")
            xmlWriter.WriteElementString("date", Nothing, "2006-11-03 12:17:09")
            For Each Datarow In TableSelected.Rows
                xmlWriter.WriteStartElement("client")
                xmlWriter.WriteElementString("id", Nothing, "2006-11-03 12:17:09")
                For Each Datarow2 In Table2Selected.Rows
                    Dim internnr As Integer =Datarow2.Item(1)
                    xmlWriter.WriteElementString("clientnr", Nothing, internnr)
                    xmlWriter.WriteStartElement("profiel")
                    For Each Datarow2 In Table3tSelected.Rows
                        xmlWriter.WriteStartElement("profiel_line")
                        xmlWriter.WriteElementString("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
ASKER CERTIFIED SOLUTION
Avatar of gpsharp
gpsharp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aatjan

ASKER

Hi G#,

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
Avatar of aatjan

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
Avatar of aatjan

ASKER

G#,

Herewith an example of the fle which is produced:-

<?xml version='1.0' encoding='ISO-8859-1'?><farmdossier><peildatum>2006-11-03 12:17:09</peildatum><client><id>2006-11-03 12:17:09</id><clientnr>3291</clientnr><profiel><profiel_line><id>0000011218</id><atk_id>14277514</atk_id><artikelnaam>ranitidine</artikelnaam><gebrtekst>2x daags 1 tablet</gebrtekst><startdat>2006-11-03 12:17:09</startdat><enddat>2006-11-03 12:17:09</enddat><apotheek_agb>8066</apotheek_agb><voorschrijver_agb>22458</voorschrijver_agb><voorschrijver>Arts/voorschrijver CL</voorschrijver><gebrcode /><gebrtekst>2x daags 1 tablet</gebrtekst><hoeveelheid>100</hoeveelheid><eenheid>ST</eenheid><hoeveelheid_tegoed>200</hoeveelheid_tegoed><hoeveelheid_herhalen_perkeer>100</hoeveelheid_herhalen_perkeer><einddatum_herhalen /><soort_medicatie>C</soort_medicatie></profiel_line></profiel></client></farmdossier>

With regards, aat jan
Avatar of aatjan

ASKER

Hi G#,

Last issue is now solved.

Dim xmlWriter As New System.Xml.XmlTextWriter(fsWriteXml, System.Text.Encoding.ASCII) did the work!

Many thanks for your quick intervention!!!

Aat Jan
Avatar of gpsharp
gpsharp

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#