• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 903
  • Last Modified:

root element is missing

Hi

I'm trying to read a DataSet from an SQL connection and then write it to an XML file and then subsequenltly reload it. These are the functions I'm using:

    Private Sub WriteXmlToFile(ByVal thisDataSet As DataSet)
        If thisDataSet Is Nothing Then
            Return
        End If
        ' Create a file name to write to.
        Dim filename As String = "c:\myXmlDoc.xml"
        ' Create the FileStream to write with.
        Dim myFileStream As New System.IO.FileStream _
           (filename, System.IO.FileMode.Create)
        ' Create an XmlTextWriter with the fileStream.
        Dim myXmlWriter As New System.Xml.XmlTextWriter _
           (myFileStream, System.Text.Encoding.Unicode)
        ' Write to the file with the WriteXml method.
        thisDataSet.WriteXml(myXmlWriter)
        myXmlWriter.Close()
    End Sub

    Private Sub ReadXmlToFile(ByVal xmlFilename As String, ByVal newDataSet As DataSet)
        ' Read the XML document back in.
        ' Create new FileStream to read schema with.
        Dim fsReadXml As New System.IO.FileStream _
           (xmlFilename, System.IO.FileMode.Open)
        ' Create an XmlTextReader to read the file.
        Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
        ' Read the XML document into the DataSet.
        newDataSet.ReadXml(myXmlReader)
        ' Close the XmlTextReader
        myXmlReader.Close()
    End Sub

I am calling the code as follows:

        Try
            WriteXmlToFile(MyDataSet)
            Dim dataSet1 As New DataSet
            ReadXmlToFile("c:\Eigenvalue\Schools.xml", dataSet1)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

The code throws an exception stating that "root element is missing".

Can anybody tell me what is wrong?

This is the XML that is created during the write statement:

<NewDataSet><Table1><ID_SCHOOL>1</ID_SCHOOL><SCHOOL_NAME>A</SCHOOL_NAME></Table1><Table1><ID_SCHOOL>2</ID_SCHOOL><SCHOOL_NAME>B</SCHOOL_NAME></Table1><Table1><ID_SCHOOL>3</ID_SCHOOL><SCHOOL_NAME>C</SCHOOL_NAME></Table1><Table1><ID_SCHOOL>4</ID_SCHOOL><SCHOOL_NAME>D</SCHOOL_NAME></Table1></NewDataSet>

Thanks

Issac
0
IssacJones
Asked:
IssacJones
2 Solutions
 
x77Commented:
You are using :   thisDataSet.WriteXml(myXmlWriter)
where  myXmlWriter is as XmlTextReader

Youn can use : DataSet.WriteXml ( Stream )  - fsReadXml  is a stream ( FileStream ).
Note that you name it   "Read" but donĀ“t restrict it.
0
 
CodeCruiserCommented:
Try passing a mode parameter to the WriteXML method

http://msdn.microsoft.com/en-us/library/ms135426.aspx
0
 
IssacJonesAuthor Commented:
x77 - what do you mean "but don't restrict it?"
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now