469,336 Members | 5,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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.Load 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 WriteObjectsToFile(string fileName,eConnectType
objectToSerialize)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(objectToSerialize.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(fileName,null);
xmlSerializer.Serialize(xmlTextWriter,objectToSeri alize);
xmlTextWriter.Close();
}

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

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

XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
XmlTextWriter tw = new XmlTextWriter(ms,null);
s.Serialize(xmlTextWriter,objectToSerialize);

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

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

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.Load 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 WriteObjectsToFile(string fileName,eConnectType
objectToSerialize)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(objectToSerialize.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(fileName,null);
xmlSerializer.Serialize(xmlTextWriter,objectToSeri alize);
xmlTextWriter.Close();
}

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

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.com> wrote in message
news:Oi**************@TK2MSFTNGP11.phx.gbl...

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

XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
XmlTextWriter tw = new XmlTextWriter(ms,null);
s.Serialize(xmlTextWriter,objectToSerialize);

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

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

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.Load 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 WriteObjectsToFile(string fileName,eConnectType
objectToSerialize)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(objectToSerialize.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(fileName,null);
xmlSerializer.Serialize(xmlTextWriter,objectToSeri alize);
xmlTextWriter.Close();
}

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

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****@budgetext.com> wrote in message
news:eh**************@TK2MSFTNGP11.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.com> wrote in message
news:Oi**************@TK2MSFTNGP11.phx.gbl...

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

XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
XmlTextWriter tw = new XmlTextWriter(ms,null);
s.Serialize(xmlTextWriter,objectToSerialize);

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

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

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.Load 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 WriteObjectsToFile(string fileName,eConnectType
objectToSerialize)
{
try
{
XmlSerializer xmlSerializer = new
XmlSerializer(objectToSerialize.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(fileName,null);
xmlSerializer.Serialize(xmlTextWriter,objectToSeri alize);
xmlTextWriter.Close();
}

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


Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bluetears76 | last post: by
4 posts views Thread by Andy Neilson | last post: by
reply views Thread by Bluetears76 | last post: by
reply views Thread by Daniel Lindros via .NET 247 | last post: by
4 posts views Thread by Ultrakorne | last post: by
1 post views Thread by Armond VanHeusen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.