472,146 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

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 17570

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
reply views Thread by leo001 | last post: by

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.