Link to home
Create AccountLog in
Avatar of oromm
orommFlag for United States of America

asked on

Root element missing when populating DataSet with XML from MemoryStream

I have an issue trying to load some simple XML into a DataSet in ASP.Net code using VB.Net.  When I attempt to read the XML from a FileStream, it loads fine.  But when loading from a MemoryStream, the ReadXML generates an error saying "The root element is missing.".  The reason I am using a MemoryStream is that the XML is being generated in the code leading up to the population of the dataset.  I don't want to have to generate a physical file for no reason.  What follows is the XML that is being loaded, and the code that replicates my issue.  

*****************************************
For this example, the following XML is saved to a file named "cons.xml":

<Users>
  <User>
    <LoginName>gsmith</LoginName>
    <LastName>Smith</LastName>
    <FirstName>George</FirstName>
  </User>
  <User>
    <LoginName>abrown</LoginName>
    <LastName>Brown</LastName>
    <FirstName>Amy</FirstName>
  </User>
</Users>


ASP.Net / VB.Net code:
*****************************************
Imports System.Xml
Imports System.io

Public Class Stuff1
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Const s_XML_FILE As String = "cons.xml"
        Dim dsCons As DataSet
        Dim consStream As FileStream
        ' read the xml in from file
        consStream = New FileStream(s_XML_FILE, FileMode.Open, FileAccess.Read)
        dsCons = New DataSet
        Try ' reading the file stream xml into the dataset
            dsCons.ReadXml(consStream)
            Response.Write("fileStream = " & dsCons.GetXml & "<br/>")
        Catch ex As Exception
            Response.Write("FileStream EXCEPTION = " & ex.Message & "<br/>")
        End Try
        dsCons.Dispose()
        dsCons = Nothing
        consStream.Close()
        consStream = Nothing
        ' *** now test the memory stream
        Dim memStream As New MemoryStream
        Dim xmlDoc As New XmlDocument
        ' this loads the xml that would normally be created in the inline code
        xmlDoc.Load(s_XML_FILE)
        ' save the xml to the memory stream
        xmlDoc.Save(memStream)
        dsCons = New DataSet
        Try ' reading the memory stream xml into the dataset
            dsCons.ReadXml(memStream)
            Response.Write("MemoryStream = " & dsCons.GetXml & "<br/>")
        Catch ex As Exception
            Response.Write("MemoryStream EXCEPTION = " & ex.Message & "<br/>")
        End Try
        dsCons.Dispose()
        dsCons = Nothing
        memStream.Close()
        memStream = Nothing
    End Sub

End Class
****************************************************
Output:
fileStream = gsmith Smith George abrown Brown Amy
MemoryStream EXCEPTION = The root element is missing.

****************************************************
Environment:
   Visual Studio.Net 2003
   .Net Framework 1.1
   Windows 2000 Advanced Server SP4
ASKER CERTIFIED SOLUTION
Avatar of laotzi2000
laotzi2000

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of oromm

ASKER

Well, I was hoping for something a bit more complicated in fixing that, but I guess that works!