473,573 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer and MemoryStream

grs
I am using some Microsoft examples that:
1. Serialize an object using XmlSerializer and write a file out to the
harddrive.
2. Read back in the file using XmlDocument.Loa d and populate a string.

What I would like to do is use MemoryStream to do this instead of writing
to the harddrive, but I am lost in the readers, writers etc.
Can someone show me how to to this using a memory stream.

thanks
grs

// Code to accept eConnectType object and write to a disk file
public void WriteObjectsToF ile(string fileName,eConne ctType
objectToSeriali ze)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(f ileName,null);
xmlSerializer.S erialize(xmlTex tWriter,objectT oSerialize);
xmlTextWriter.C lose();
}

// code reading the file and creating the string.
string aXmlString;
try
{
// Load the written xml document
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(fileName);
aXmlString = xmlDocument.Out erXml;
Nov 12 '05 #1
3 17717

This should work:
// Create the stream.
MemoryStream ms = new MemoryStream();

XmlSerializer s = new XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter tw = new XmlTextWriter(m s,null);
s.Serialize(xml TextWriter,obje ctToSerialize);

// Rewind the Stream.
ms.Seek(0,SeekO rigin.Begin);

XmlDocument doc = new XmlDocument();
// load from stream;
doc.Load(m);
String xmlString = xmlDocument.Out erXml;

Reagrds,
grs wrote:
I am using some Microsoft examples that:
1. Serialize an object using XmlSerializer and write a file out to the
harddrive.
2. Read back in the file using XmlDocument.Loa d and populate a string.

What I would like to do is use MemoryStream to do this instead of writing
to the harddrive, but I am lost in the readers, writers etc.
Can someone show me how to to this using a memory stream.

thanks
grs

// Code to accept eConnectType object and write to a disk file
public void WriteObjectsToF ile(string fileName,eConne ctType
objectToSeriali ze)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(f ileName,null);
xmlSerializer.S erialize(xmlTex tWriter,objectT oSerialize);
xmlTextWriter.C lose();
}

// code reading the file and creating the string.
string aXmlString;
try
{
// Load the written xml document
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(fileName);
aXmlString = xmlDocument.Out erXml;

Nov 12 '05 #2
grs
M. Said,
I thank you, I had exactly the same code as you recommended BUT
I did not Rewind the Stream !! I would have struggled forever.
grs
"M. Said" <mu*****@mail.c om> wrote in message
news:Oi******** ******@TK2MSFTN GP11.phx.gbl...

This should work:
// Create the stream.
MemoryStream ms = new MemoryStream();

XmlSerializer s = new XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter tw = new XmlTextWriter(m s,null);
s.Serialize(xml TextWriter,obje ctToSerialize);

// Rewind the Stream.
ms.Seek(0,SeekO rigin.Begin);

XmlDocument doc = new XmlDocument();
// load from stream;
doc.Load(m);
String xmlString = xmlDocument.Out erXml;

Reagrds,
grs wrote:
I am using some Microsoft examples that:
1. Serialize an object using XmlSerializer and write a file out to the
harddrive.
2. Read back in the file using XmlDocument.Loa d and populate a string.

What I would like to do is use MemoryStream to do this instead of writing to the harddrive, but I am lost in the readers, writers etc.
Can someone show me how to to this using a memory stream.

thanks
grs

// Code to accept eConnectType object and write to a disk file
public void WriteObjectsToF ile(string fileName,eConne ctType
objectToSeriali ze)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(f ileName,null);
xmlSerializer.S erialize(xmlTex tWriter,objectT oSerialize);
xmlTextWriter.C lose();
}

// code reading the file and creating the string.
string aXmlString;
try
{
// Load the written xml document
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(fileName);
aXmlString = xmlDocument.Out erXml;

Nov 12 '05 #3
Ya, when I think of Streams, I always think of a VCR tape. If you record a
show, you must rewind to play it. (i.e. read it.) Otherwise, you get nothing
(or some other show) <g>

Good luck.

"grs" <gs****@budgete xt.com> wrote in message
news:eh******** ******@TK2MSFTN GP11.phx.gbl...
M. Said,
I thank you, I had exactly the same code as you recommended BUT
I did not Rewind the Stream !! I would have struggled forever.
grs
"M. Said" <mu*****@mail.c om> wrote in message
news:Oi******** ******@TK2MSFTN GP11.phx.gbl...

This should work:
// Create the stream.
MemoryStream ms = new MemoryStream();

XmlSerializer s = new XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter tw = new XmlTextWriter(m s,null);
s.Serialize(xml TextWriter,obje ctToSerialize);

// Rewind the Stream.
ms.Seek(0,SeekO rigin.Begin);

XmlDocument doc = new XmlDocument();
// load from stream;
doc.Load(m);
String xmlString = xmlDocument.Out erXml;

Reagrds,
grs wrote:
I am using some Microsoft examples that:
1. Serialize an object using XmlSerializer and write a file out to the
harddrive.
2. Read back in the file using XmlDocument.Loa d and populate a string.

What I would like to do is use MemoryStream to do this instead of writing to the harddrive, but I am lost in the readers, writers etc.
Can someone show me how to to this using a memory stream.

thanks
grs

// Code to accept eConnectType object and write to a disk file
public void WriteObjectsToF ile(string fileName,eConne ctType
objectToSeriali ze)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(o bjectToSerializ e.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(f ileName,null);
xmlSerializer.S erialize(xmlTex tWriter,objectT oSerialize);
xmlTextWriter.C lose();
}

// code reading the file and creating the string.
string aXmlString;
try
{
// Load the written xml document
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(fileName);
aXmlString = xmlDocument.Out erXml;


Nov 12 '05 #4

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

Similar topics

1
4306
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting FileMessage or ChatMessage. So how to get that object and use it I have written following code for serialization public void Send(Message message) {...
2
13532
by: Mullin Yu | last post by:
I am using XmlSerializer to serialize an object to xml string like the following at C#. Now, I want to know how can I 1. change the default encoding to utf-8 or others, instead of utf-16 2. remove the XML declaration => <?xml version ....> 3. remove the white space / tab of the xml structure XmlSerializer( typeof( PrinterSetting ) ) ...
1
12778
by: PMCG | last post by:
Hi I am using the XMLSerializer class to serialize a custom class into a memory stream whose contents i then pass to a MSSQL SP that uses the sp_xml_preparedocument SP, i can serialize the class but get secape characters in the XML when i read from the memory stream. I do not know much about encoding so i may be missing something very obvious...
4
11360
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This should have worked fine since the class in question was already being serialized and deserialized as part of a Web service interface. What I found was...
0
1102
by: Bluetears76 | last post by:
Now Hierarchy is that there is a class named "Student" There are two child classes "GraduateStudent" and "HighSchoolStudent". I worte the following code for serializing the student and deserializing. Now the problem is that as i do not know the type of Student while deserializing so i get an error on line student=...
0
1655
by: Daniel Lindros via .NET 247 | last post by:
Hi! I am currently trying to learn C#, and have started by developinga simple messaging application with a client and server. Byusing callback delegates, I can asynchronously send and recievetext as byte arrays. Because I wanted to be able to send more advanced messagesbetween the client and server, I decided to create my ownmessage class,...
4
5401
by: Ultrakorne | last post by:
hi, i have some problems with my client talk to my server... i am using xmlserializer to serialize object and send them to the other side of the connection. I need to send / recive by both client and server. client after login waits all the time listening for objects on a thread, and sends objects on users events on the main thread. server...
1
1199
by: Armond VanHeusen | last post by:
I have an application with a bunch of DLLs that are loaded in as plugins. In one of these I am trying to get XMLSerialization to work but with little success. I then tried some very simple code shown below as a quick and dirty test. The kicker is that it works perfectly in the EXE and every DLL except one. In the one I need it to as a matter...
14
1874
by: Joe | last post by:
this file is drawn in VB.NET and input output goes by XmlSerializer. Therefore, simple output file looks like: <?xml version="1.0" encoding="utf-8"?> <DrawablePicture xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" BackColor="-1250856"> <DrawableRectangle LineWidth="2" X1="377" Y1="88"...
0
8031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8204
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...
1
7795
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...
0
6424
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...
1
5600
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...
0
5294
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...
0
3735
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1309
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.