The root element is missing when binding dataset to xml textreader

T

Tulio Quinones

cross posted to microsoft.public.dotnet.xml

Hi,

I am attempting to create a xml file to a memory stream the bind it to a dataset. I can write a physical file with no problem. However I recieve the following error when trying to bind it to a dataset:

Exception Details: System.Xml.XmlException: The root element is missing.

Here is my code:

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(ms, Encoding.UTF8);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("menu");
xmlWriter.WriteStartElement("menuItem");
xmlWriter.WriteElementString("text", "NewArticles");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
ms.Position = 0;

XmlTextReader xxx = new XmlTextReader(ms);
DataSet ds = new DataSet();

ds.ReadXml(xxx); <------------- Error here

ms.Close();
Menu1.DataSource = ds;
Menu1.DataBind();

Source Error:

Line 43:
Line 44: DataSet ds = new DataSet();
Line 45: ds.ReadXml(xxx);
Line 46: ms.Close();
Line 47:

Source File: c:\inetpub\wwwroot\ipass\header.ascx.cs Line: 45

Stack Trace:

[XmlException: The root element is missing.]
System.Xml.XmlTextReader.Read() +876
System.Xml.XmlReader.MoveToContent() +75
System.Data.DataSet.ReadXml(XmlReader reader, Boolean denyResolving)
System.Data.DataSet.ReadXml(XmlReader reader)
iPass.header.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\ipass\header.ascx.cs:45
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Page.ProcessRequestMain() +731
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top