473,614 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading xml from MemoryStream causes exception


PREVIOUS BACKGROUND POST:

I am trying to reuse a Memory Stream for loading and
transforming the xml it contains

Basically I have defined following interfaces:

Class Render
{

// Memory stream declared inside class so that it is
accessible by all the methods
// It acts as the persistent storage for the life of the
instance of the class
// the point is to have one storage that can be Reloaded w
new XML and re-used

System.IO.Memor yStream xstream = new System.IO.Memor yStream
();

//Constructor

Render(...)
{
.....
//Save XmlDocument to a Memory Stream
XmlDoc.Save(xst ream);
- Constructor Render
public Render(XmlReade r CleanXmlDoc,str ing SeriesXSL)

- It Loads 2 things:
the XML
the XSL

and XML is saved to the stream using an XmlDocument to
Load the XmlReader and then save it to a stream

these objects are used by the following Methods using
Xsl.Transform method and other stuff

Methods:

public string RenderSectionPr eview()
public string RenderHTML(Stri ng RenderMode)
public string RenderPDF(Strin g RenderMode)

I am using

try
{
xstream.Positio n = 0;
//Console.Write(x stream.Length.T oString());
XpDoc = new XPathDocument(x stream); //<-KEY STATEMENT
}
catch(Exception e)
{
Console.WriteLi ne ("Exception: {0} - Render HTML -
XPathDoc ", e.ToString());
return e.ToString();
}

inside the RenderHTML

It works one time and renders an HTML file
It errors out the 2nd call at the XPathDocument Statement
due to a MemoryStream error

as shown below:

- Class Call Render - Main **

61600
61600Exception: System.ObjectDi sposedException : Cannot
access a closed Stream.
at System.IO.__Err or.StreamIsClos ed()
at System.IO.Memor yStream.set_Pos ition(Int64 value)
at Luau.Render.Ren der.RenderHTML( String RenderMode) in
c:\construction \code\r
endering\render com\render.cs:l ine 241 - Render HTML -
XPathDoc
Exception: System.ObjectDi sposedException : Cannot access a
closed Stream.
at System.IO.__Err or.StreamIsClos ed()
at System.IO.Memor yStream.set_Pos ition(Int64 value)
at Luau.Render.Ren der.RenderHTML( String RenderMode) in
c:\construction \code\r
endering\render com\render.cs:l ine 241 - Render HTML -
XPathDoc

BACKGROUND INFORMATION PREVIOUS POST AND SUGGESTION TO USE
MEMORYSTREAM:
If I understand everything correctly, then you problem is
that you are
re-using the XmlReader that you attached to the
MemoryStream.

An XmlReader is forward only, i.e. you cannot rewind it!
You are on the
right track with using a MemoryStream though. You can
rewind the
MemoryStream by setting Position = 0. Then you can attach
a fresh
Xml(Text)Reader an re-read the stream.

Did I understand your problem and did this help?

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"xmlguy" <xm****@yahoo.c om> wrote in message
news:00******** *************** *****@phx.gbl.. .
Cant seem to solve this problem

I need to be able to re-use XmlReader and XPathDocument
for an XSLT Transform.

Basically I have defined following interfaces:

Class Render

(Common and public inside the class)
XPathDocument XpDoc;
XmlReader XrDoc;

- Constructor Render
public Render(XmlReade r CleanXmlDoc,str ing SeriesXSL)

- It Loads 2 things:
the XML
the XSL

these objects are used by the following Methods using
Xsl.Transform method and other stuff

Methods:

public string RenderSectionPr eview()
public string RenderHTML(Stri ng RenderMode)
public string RenderPDF(Strin g RenderMode)

I was using
XPathDocument XpDoc = new XPathDocument(X mlReader);
inside the constructor and XpDoc was used as source for
XSLT translations in RenderHTML and RenderPDF.

Everything was working fine.

Now I decided to add 2 more methods to allow us to use the same RenderObject (our custom class) and UPDATE the XML by using the following Method ReloadXML.

Also wanted to add a WriteXML object that would allow us
to write the incoming XMLReader (for debug as well as
saving it)

New Methods:

public int ReloadXML(XmlRe ader CleanXmlDoc1)
public int WriteXML(String RenderMode)

The problem is in this process we are calling the

XPathDocument XpDoc = new XPathDocument(X mlReader);

with CleanXmlDoc1 (the new XML)

again.

We were hoping that the XpDoc would get populated with it
and we could use the XpDoc again as before in the
Xsl.Transform()

ANALYSIS:

After a lot of head breaking have come to the conclusion
that this has something to do with XPathDocument or
XmlReader and the current position of the cursor etc. or
something like that ? BUT I MAY BE COMPLETELY WRONG.
Is it something like this ? What is the issue ?

I tried using MemoryStream to store the XmlReader and then use XPathDocument but logically I am still not able to re- populate the XPathDocument XpDoc with a new XmlReader.

Kindly advise. Thanks.


Nov 11 '05 #1
1 11157
xmlguy wrote:

I am using

try
{
xstream.Positio n = 0;
//Console.Write(x stream.Length.T oString());
XpDoc = new XPathDocument(x stream); //<-KEY STATEMENT 61600Exception: System.ObjectDi sposedException : Cannot
access a closed Stream.


That's not enough to set MemoryStream's position, because the stream is
in closed state after first XmlReader (within XPathDocument constructor)
read it to the end.

You cannot reopen MemoryStream, but you can reuse its internal buffer.
The data within MemoryStream is stored as array of bytes, so to reuse
the XML, which is stored there you have to create new MemoryStream on
that buffer. Someting like

MemoryStream newStream = new MemoryStream(xs tream.GetBuffer ());
XpDoc = new XPathDocument(n ewStream);

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2649
by: cnu | last post by:
Hi I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading images to db works fine for me. Reading from db to byte is also ok. But, when I try to display them in my form controls, I get this error. This is how it goes : <code> byte bImage1 = (byte)datasetImageList.Tables.Rows; System.IO.MemoryStream ms = new MemoryStream(bImage1, 0, bImage1.Length); this.lblRunImage.Image = System.Drawing.Image.FromStream(ms); //...
2
5873
by: D | last post by:
Hi, I am trying to instantiate an instance of XPathDocument after serializing XML into a memory stream. Here is my method that uses XmlSerializer public MemoryStream SerializeXMLSelectedApplication() { System.IO.MemoryStream stream = new MemoryStream(); try
3
9503
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
1
5567
by: Arcnet | last post by:
Using MemoryStream I Have a problem to create a new Image from byte array that originaly was created from an older image (Everything is being preformed in the same Thread) //Get Image From File byte arr;
3
5862
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the web (using ASP.NET). However, when I generate the Image object using FromStream (passing in the MemoryStream containing image bytes), an exception of "A generic error occurred in GDI+" is thrown when performing the conversion/save for display....
5
26255
by: Naamat | last post by:
Hello, I am the sample FPSEPublish (http://blog.baeke.info/blog/_archives/2005/3/3/393158.html) code to upload a document to Sharepoint (WSS). This works perfectly for samll documents. Problem: When I attempt to upload a huge document (300Megabayte) on a PC with 3.6Gig RAM I am getting a OutOfMemoryException when the program attempts to read the
3
2010
by: apiringmvp | last post by:
All, So I am creating a function that gets a short blurb of html from a blog. I would like to retain all html formating and images. The code below works well, with the exception of one issue. My issue: --------------------- When a blog's html has attributes with no quotes i get an exception.
5
4627
by: sascha.folville | last post by:
Hi, I've some trouble reading the containing frames of a multiframe tiff. First frame is a 1700X2400 px CCITT4, second in JPG 820x1200, third is a CCITT again ... and so on. My function extracts the CCITT-frames correct, but the second, fourth, etc. frames causes a general GDI+ error. Hope someone can help!
3
5548
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an error regarding "the proxy has no channel sink.......or no suitable Client channel to talk to the server."
0
8197
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
8142
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
8589
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
8287
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,...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
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
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2573
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
1
1757
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.