Serialization

  • Thread starter Thread starter Janne
  • Start date Start date
J

Janne

I have a problem with Xml and Serialization.

I use serialization to get Xml as a result. All
Serialization examples in Microsoft documentation writes
the result to a file.

I don't want a file, I want an XML-object (XmlNode)
instead that I can pass as a parameter to another method.

How can I do that?

Example code:

XmlSerializer mySerializer = new XmlSerializer(typeof
(myClass));
TextWriter myWriter = new StreamWriter(@"C:\myfile.xml");

mySerializer.Serialize(myXmlWriter, myNode);
myWriter.Close();
 
Janne,

Instead of using the constructor for a StreamWriter that takes a file
name, use a MemoryStream, and pass that, like this:

// The memory stream that will hold the xml.
MemoryStream pobjStream = new MemoryStream();

// Create the StreamWriter around the memory stream.
TextWriter myWriter = new StreamWriter(pobjStream);

// Now save to the memory stream.
mySerializer.Serialize(myXmlWriter, myNode);

Now, if you want to get that into an Xml document, you can call the Load
method, passing a stream to it.

// Create the xml document.
XmlDocument pobjXmlDoc = new XmlDocument();

// Load the content.
pobjXmlDoc.Load(pobjStream);

XmlDocument derives from XmlNode so you can just pass that around to
anything that accepts an XmlNode instance.

Hope this helps.
 
Nicholas!

Thank you for your help but it still won't work.

Is there a way to check the Stream-object or the Writer-
object? The load-command gives me an empty XmlDocument.

Take a look at the code and check please.
MemoryStream pobjStream = new MemoryStream();

TextWriter myWriter = new StreamWriter(pobjStream);

mySerializer.Serialize(myWriter, myNode);
XmlDocument pobjXmlDoc = new XmlDocument();

pobjXmlDoc.Load(pobjStream);



-----Original Message-----
Janne,

Instead of using the constructor for a StreamWriter that takes a file
name, use a MemoryStream, and pass that, like this:

// The memory stream that will hold the xml.
MemoryStream pobjStream = new MemoryStream();

// Create the StreamWriter around the memory stream.
TextWriter myWriter = new StreamWriter(pobjStream);

// Now save to the memory stream.
mySerializer.Serialize(myXmlWriter, myNode);

Now, if you want to get that into an Xml document, you can call the Load
method, passing a stream to it.

// Create the xml document.
XmlDocument pobjXmlDoc = new XmlDocument();

// Load the content.
pobjXmlDoc.Load(pobjStream);

XmlDocument derives from XmlNode so you can just pass that around to
anything that accepts an XmlNode instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a problem with Xml and Serialization.

I use serialization to get Xml as a result. All
Serialization examples in Microsoft documentation writes
the result to a file.

I don't want a file, I want an XML-object (XmlNode)
instead that I can pass as a parameter to another method.

How can I do that?

Example code:

XmlSerializer mySerializer = new XmlSerializer(typeof
(myClass));
TextWriter myWriter = new StreamWriter (@"C:\myfile.xml");

mySerializer.Serialize(myXmlWriter, myNode);
myWriter.Close();


.
 
Janne,

The MemoryStream class has a ToArray method you can use to return the
byte array that represents the bytes in the stream.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Janne said:
Nicholas!

Thank you for your help but it still won't work.

Is there a way to check the Stream-object or the Writer-
object? The load-command gives me an empty XmlDocument.

Take a look at the code and check please.
MemoryStream pobjStream = new MemoryStream();

TextWriter myWriter = new StreamWriter(pobjStream);

mySerializer.Serialize(myWriter, myNode);
XmlDocument pobjXmlDoc = new XmlDocument();

pobjXmlDoc.Load(pobjStream);



-----Original Message-----
Janne,

Instead of using the constructor for a StreamWriter that takes a file
name, use a MemoryStream, and pass that, like this:

// The memory stream that will hold the xml.
MemoryStream pobjStream = new MemoryStream();

// Create the StreamWriter around the memory stream.
TextWriter myWriter = new StreamWriter(pobjStream);

// Now save to the memory stream.
mySerializer.Serialize(myXmlWriter, myNode);

Now, if you want to get that into an Xml document, you can call the Load
method, passing a stream to it.

// Create the xml document.
XmlDocument pobjXmlDoc = new XmlDocument();

// Load the content.
pobjXmlDoc.Load(pobjStream);

XmlDocument derives from XmlNode so you can just pass that around to
anything that accepts an XmlNode instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a problem with Xml and Serialization.

I use serialization to get Xml as a result. All
Serialization examples in Microsoft documentation writes
the result to a file.

I don't want a file, I want an XML-object (XmlNode)
instead that I can pass as a parameter to another method.

How can I do that?

Example code:

XmlSerializer mySerializer = new XmlSerializer(typeof
(myClass));
TextWriter myWriter = new StreamWriter (@"C:\myfile.xml");

mySerializer.Serialize(myXmlWriter, myNode);
myWriter.Close();


.
 
Back
Top