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

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

<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
0
oromm
Asked:
oromm
1 Solution
 
laotzi2000Commented:
That's because memStream is at the end of the file
You should call memStream.Seek(0, SeekOrigin.Begin)
before readxml
0
 
orommAuthor Commented:
Well, I was hoping for something a bit more complicated in fixing that, but I guess that works!  
0

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