XML Document Serialization

G

Guest

Hi,

Is it possible to serialize a property of XmlDocument type within a class?
For example, I'm able to serialize the following class for the "Items"
property. But when I include a property that is of XMLDocument type I will
get an error.

How would you go about this?

using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("TestList")]
public class TestList {

public TestList() {
}

[XmlElement("item")]
public string Items {
get {
...
}
set {
...
}
}

[XmlElement("XmlDoc")]
public XmlDocument XmlDoc {
get {
...
}
set {
...
}
}

}
 
D

Dave Sexton

Hi,

Apparently they've fixed the problem since 1.0. I tested it using the 2.0
framework and it worked fine.

What's strange is that even in 2.0 XmlDocument isn't marked with
SerializableAttribute and typeof(XmlDocument).IsSerializable returns false.

If you can't use the 2.0 framework, try placing XmlIgnoreAttribute on the
XmlDocument property and create a serializable String property instead
named, "Xml", for instance. Hook up the set accessor on the "Xml" property
so that it loads the specified value into an XmlDocument and create the get
accessor so that it reads the XmlDocument.

Here's the code that I tested, which worked in 2.0:

class Program
{
static void Main()
{
TestList originalObj, obj = new TestList();
originalObj = obj;

obj.Items = "Some Item";

obj.XmlDoc = new XmlDocument();
obj.XmlDoc.LoadXml(xml); // xml imported from a file

Console.WriteLine("Serializing and deserializing...");

using (MemoryStream stream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(TestList));

serializer.Serialize(stream, obj);

stream.Position = 0;

obj = (TestList) serializer.Deserialize(stream);
}

Debug.Assert(originalObj != obj, "References are equal after
deserialization");
Debug.Assert(obj.Items == "Some Item", "Invalid Items Property");

Debug.Assert(obj.XmlDoc != null, "Deserialized XmlDoc is null");
Debug.Assert(obj.XmlDoc.OuterXml != null, "Invalid XmlDoc OuterXml");

Console.WriteLine();
Console.WriteLine("Deserialized Document: ");
Console.WriteLine();
Console.WriteLine(obj.XmlDoc.OuterXml);
Console.ReadLine();
}
}

[XmlRoot("TestList")]
public class TestList
{
public TestList()
{
}

[XmlElement("item")]
public string Items
{
get
{
return items;
}
set
{
items = value;
}
}

private string items = "Default Items";

[XmlElement("XmlDoc")]
public XmlDocument XmlDoc
{
get
{
return doc;
}
set
{
doc = value;
}
}

private XmlDocument doc;
}

--
Dave Sexton

et_ck said:
I'm trying to send the class to a queue (MSMQ) but it throws the following
error while trying to queue the class.


et_ck said:
Hi Dave,

Sorry for the late reply.

I got the following error:

System.Runtime.Serialization.SerializationException: The type XmlDocument
in
Assembly System.Xml, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089 is not marked as serializable.

Are there extra settings to set?

Regards.

Dave Sexton said:
Hi,

That should work. What is the error?

--
Dave Sexton

Hi,

Is it possible to serialize a property of XmlDocument type within a
class?
For example, I'm able to serialize the following class for the
"Items"
property. But when I include a property that is of XMLDocument type I
will
get an error.

How would you go about this?

using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("TestList")]
public class TestList {

public TestList() {
}

[XmlElement("item")]
public string Items {
get {
...
}
set {
...
}
}

[XmlElement("XmlDoc")]
public XmlDocument XmlDoc {
get {
...
}
set {
...
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top