473,405 Members | 2,272 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,405 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 10110
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();
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.