473,700 Members | 2,505 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 17727

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
4317
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) { NetworkStream netWorkStream=null;
2
13538
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 ) ) But, I got the something like the following:
1
12799
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 here, heres some of the cod _Serializer = new XmlSerializer(typeof(MyClass)) _Stream = new...
4
11389
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 that by deserializing from an XmlNodeReader instead of an XmlTextReader, XML Serialization doesn't work...
0
1104
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= (Student)deserializer.Deserialize(memoryStream); Any help will be highly appriciated
0
1659
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, serialize it, and deserialize it at the other end(using XmlSerializer). Now to my question: ...
4
5411
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 waits connections, start a new thread for each connection and after validating login waits for...
1
1207
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 of fact. The symptoms is that no exception is thrown unless I am in the Exceptions window (the...
14
1880
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" X2="323" Y2="130" ForeColor="-16777216" BackColor="-1" /> </DrawablePicture>
0
8729
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
9217
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9078
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...
0
8928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5903
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
4407
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2393
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2031
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.