root element is missing

Posted on 2011-10-31
Last Modified: 2012-05-12

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
        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.
    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.
        ' Close the XmlTextReader
    End Sub

I am calling the code as follows:

            Dim dataSet1 As New DataSet
            ReadXmlToFile("c:\Eigenvalue\Schools.xml", dataSet1)
        Catch ex As Exception
        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:



Question by:IssacJones
    LVL 15

    Accepted Solution

    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.
    LVL 83

    Assisted Solution

    Try passing a mode parameter to the WriteXML method

    Author Comment

    x77 - what do you mean "but don't restrict it?"

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now