473,666 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.XmlE xception: 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.XPat h;
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(ms tream);

xslt.Transform( mydata,null,wri ter,null);
DataSet ds = new DataSet();
ds.ReadXml(mstr eam);
foreach (DataTable dt in ds.Tables)
foreach ( DataRow dr in dt.Rows)
foreach (DataColumn dc in dt.Columns)
Console.WriteLi ne(dr[dc].ToString());
re.Close();

}

}

}
Nov 16 '05 #1
2 10130
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,wri ter,null);

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

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Reshma Prabhu" <Re**********@d iscussions.micr osoft.com> wrote in message
news:F6******** *************** ***********@mic rosoft.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.XmlE xception: 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.XPat h;
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(ms tream);

xslt.Transform( mydata,null,wri ter,null);
DataSet ds = new DataSet();
ds.ReadXml(mstr eam);
foreach (DataTable dt in ds.Tables)
foreach ( DataRow dr in dt.Rows)
foreach (DataColumn dc in dt.Columns)
Console.WriteLi ne(dr[dc].ToString());
re.Close();

}

}

}

Nov 16 '05 #2
Reshma Prabhu <Re**********@d iscussions.micr osoft.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.XmlE xception: 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.Positio n = 0;

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

--
Jon Skeet - <sk***@pobox.co m>
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
285
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 your Visual Studio and compile it + run it; so you can see what the actual problem is. Thanks. code:
3
3358
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 Xml by using XmlTextWriter to the stream Response.Clear(); Response.AppendHeader("Content-Disposition", "attachment; filename=BaldrianOrdersMamut.xml");
7
5487
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) { Byte bytearrayinput = StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); //DES instance System.Security.Cryptography.TripleDESCryptoServiceProvider des = new
0
1032
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 this report into PDF without saving any temporary file on server disk. I'm using such code: P.S. At moment when this code is running I already have "crReportDocument" created and assigned all required parameters to it.
1
2127
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 parsing was completed.# The second solution is functionall - I am using in this solution FileStream - everything works fine !! I want to use MemoryStream instead of FileStrem Can somebody tells me where I have problem ? I am using the same...
2
1056
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 (string.chars(indexOfExceedingChars) is nothing) To correct the problem, I used SomeOtherStringVar = trim(DecryptedString) The problem is the following: Trim(DecryptedString) returns the string
7
3105
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
3653
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 written in file are not the sent bytes. Copy and paste the following lines to observe my problem. What can I do to resolve problem ? Only System.Text.Encoding.ASCII write the same number of bytes, but not the good bytes. Someone can help me. Thanks by...
1
3816
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 7 or 10 times then my computer's memory gets flooded and every time I call the Clone() method the processor resources gets consumed 100% for like 30 seconds. And this problem increases as the amount of clones are created increases. I read some...
4
8420
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
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7381
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.