Re: XML Document Serialization



Hi Dave,

Thanks for the reply and sorry once again for the late reply. I was rushing
for a solution on this and created a work around for my problem.

I'll be testing out the solution you gave.

Thank you for your time.

"Dave Sexton" wrote:

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" <etck@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:80C8D237-127B-4B55-8A8B-2DA0DDEF9C7F@xxxxxxxxxxxxxxxx
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" wrote:

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" wrote:

Hi,

That should work. What is the error?

--
Dave Sexton

"et_ck" <etck@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:20719166-123A-46C9-B6C7-DA8BDE11B256@xxxxxxxxxxxxxxxx
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 {
...
}
}

}






.



Relevant Pages

  • Re: XML Document Serialization
    ... XmlDocument property and create a serializable String property instead ... TestList originalObj, obj = new TestList; ... I'm able to serialize the following class for the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XmlSerializer help
    ... XmlDocument then it's the way to go. ... >> take a look at Chris Lovett's XmlNodeWriter on www.gotdotnet.com. ... >> Serialize method. ... >>> public string information; ...
    (microsoft.public.dotnet.xml)
  • Re: Serializing XmlDocument to SerializationInfo
    ... That depends on the reason why you are serializing the XmlDocument and its containing object in the first place. ... the XmlDocument to a file, possibly on a web server, and serialize a path string. ... You might have to download the document asynchronously, ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Create an XmlDocument from XmlSerializer?
    ... Create an empty XmlDocument, like this ... If you don't like using the XmlNodeWriter then alternatively, ... dispatch with the File altogether and serialize to a String or Memory- ... Stream and wrap ...
    (microsoft.public.dotnet.xml)
  • Re: XML Document Serialization
    ... "Dave Sexton" wrote: ... Is it possible to serialize a property of XmlDocument type within a class? ...
    (microsoft.public.dotnet.languages.csharp)

Loading