473,372 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 10102
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
3
by: Olav Tollefsen | last post by:
I'm using the following code to create an Xml document to be saved by the browser (IE): MemoryStream stream = new MemoryStream(); OrderManager.ExportToXml(stream); // Creates...
7
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
0
by: David Dvali | last post by:
Hello. I have some strange problem and will be very thankful if anybody will help me to solve it. In my ASP.NET web form I have used CrystalReport to produce some report, now I want to export...
1
by: Pavel Pavel | last post by:
I have 2 solution to deserialize ArrayList which contains objects I am using for this MemoryStream, but MemoryStream is not functional - I receive always error #End of Stream encountered before...
2
by: ThunderMusic | last post by:
Hi, I encrypted a string using the RijndaelManaged class. When decrypted, it happens that the string is longer then the original, but the exceeding chars are nothing...
7
by: redraven | last post by:
Hi, I have a webservice with a method that returns an instance of System.IO.MemoryStream. For example: public MemoryStream myMeth() { return new MemoryStream();
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
1
by: Alex D. | last post by:
hi guys. I need to clone multiple times an object and I am succesfully cloning using the regular serialization process, using a MemoryStream. My problem is that after cloning the object more that...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.