472,110 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Problem with MemoryStream

hello,
I am trying to do an xsl tranformation from an XML file into
another xml file. I want the output file to be in MemoryStream so that my
dataset can direclty read xml using
dataset.ReadXml(memoryStream).

But at the time of reading it gives following exception
System.Xml.XmlException: The root element is missing.

But if i write into a file and then read dataset from that then it works fine.

PLEASE HELP ME

The whole code is attached below

Thanks,
The whole code is

using System;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using System.IO;
using System.Data;

namespace XslMapper
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("Abc.xsl");

//Create a new XPathDocument and load the XML data to be
transformed.
XPathDocument mydata = new XPathDocument("XYZ.xml");

MemoryStream mstream = new MemoryStream();

//Create an XmlTextWriter which outputs to the console.
StreamWriter writer = new StreamWriter(mstream);

xslt.Transform(mydata,null,writer,null);
DataSet ds = new DataSet();
ds.ReadXml(mstream);
foreach (DataTable dt in ds.Tables)
foreach ( DataRow dr in dt.Rows)
foreach (DataColumn dc in dt.Columns)
Console.WriteLine(dr[dc].ToString());
re.Close();

}

}

}
Nov 16 '05 #1
2 9975
Reshma,

The problem here is that you are not resetting the position in the
stream back to the beginning, and therefore, the dataset bombs when trying
to read the contents. After the call to Transform, you have to reset the
stream, like this:

// Transform.
xslt.Transform(mydata,null,writer,null);

// Reset the stream pointer.
mstream.Seek(0, SeekOrigin.Begin);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Reshma Prabhu" <Re**********@discussions.microsoft.com> wrote in message
news:F6**********************************@microsof t.com...
hello,
I am trying to do an xsl tranformation from an XML file into
another xml file. I want the output file to be in MemoryStream so that my
dataset can direclty read xml using
dataset.ReadXml(memoryStream).

But at the time of reading it gives following exception
System.Xml.XmlException: The root element is missing.

But if i write into a file and then read dataset from that then it works
fine.

PLEASE HELP ME

The whole code is attached below

Thanks,
The whole code is

using System;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using System.IO;
using System.Data;

namespace XslMapper
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
//Create a new XslTransform object.
XslTransform xslt = new XslTransform();

//Load the stylesheet.
xslt.Load("Abc.xsl");

//Create a new XPathDocument and load the XML data to be
transformed.
XPathDocument mydata = new XPathDocument("XYZ.xml");

MemoryStream mstream = new MemoryStream();

//Create an XmlTextWriter which outputs to the console.
StreamWriter writer = new StreamWriter(mstream);

xslt.Transform(mydata,null,writer,null);
DataSet ds = new DataSet();
ds.ReadXml(mstream);
foreach (DataTable dt in ds.Tables)
foreach ( DataRow dr in dt.Rows)
foreach (DataColumn dc in dt.Columns)
Console.WriteLine(dr[dc].ToString());
re.Close();

}

}

}

Nov 16 '05 #2
Reshma Prabhu <Re**********@discussions.microsoft.com> wrote:
I am trying to do an xsl tranformation from an XML file into
another xml file. I want the output file to be in MemoryStream so that my
dataset can direclty read xml using
dataset.ReadXml(memoryStream).

But at the time of reading it gives following exception
System.Xml.XmlException: The root element is missing.


That's because you're writing the data to the memory stream, and then
trying to read from it without repositioning the "cursor" of the stream
to the start.

Put

mstream.Position = 0;

just before the call to ReadXml and you'll be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by crawlerxp | last post: by
3 posts views Thread by Olav Tollefsen | last post: by
7 posts views Thread by c duden | last post: by
1 post views Thread by Pavel Pavel | last post: by
1 post views Thread by Alex D. | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.