Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

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":


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
            Response.Write("fileStream = " & dsCons.GetXml & "<br/>")
        Catch ex As Exception
            Response.Write("FileStream EXCEPTION = " & ex.Message & "<br/>")
        End Try
        dsCons = Nothing
        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
        ' save the xml to the memory stream
        dsCons = New DataSet
        Try ' reading the memory stream xml into the dataset
            Response.Write("MemoryStream = " & dsCons.GetXml & "<br/>")
        Catch ex As Exception
            Response.Write("MemoryStream EXCEPTION = " & ex.Message & "<br/>")
        End Try
        dsCons = Nothing
        memStream = Nothing
    End Sub

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

   Visual Studio.Net 2003
   .Net Framework 1.1
   Windows 2000 Advanced Server SP4
1 Solution
That's because memStream is at the end of the file
You should call memStream.Seek(0, SeekOrigin.Begin)
before readxml
orommAuthor Commented:
Well, I was hoping for something a bit more complicated in fixing that, but I guess that works!  

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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