Root element missing when populating DataSet with XML from MemoryStream

Posted on 2005-03-01
Medium Priority
Last Modified: 2012-06-21
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
Question by:oromm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

laotzi2000 earned 1000 total points
ID: 13436474
That's because memStream is at the end of the file
You should call memStream.Seek(0, SeekOrigin.Begin)
before readxml

Author Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 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