XML Serialization of Two Classes

G

Guest

I have two classes that are XML Serializable, lets just call them Class A &
B. I would like to expose Class B as a property of Class A and have both
classes simply serialize with Class B as an element (and then serialize
itself as it normally would). How can I accomplish this? (More specifically,
what's the attributes for the property to serialize correctly.) Basically I
want the following XML:
<ClassA><MySubClass><MySubClassNode/></MySubClass></ClassA>

Code Example:
<XmlRoot()>_
Public Class ClassA
Private _subClassB As ClassB
<??????????????????>_
Public Property MySubClass As ClassB
Get
Return _classB
Set ETC...
End Property
End Class

<XmlRoot()>_
Public Class B
Private _var As String=""

<XmlElement("MySubClassNode")>_
Public Property MySubClassNode As String
Get
Return _var
ETC...
End Property
End Class
 
G

Guest

Hi Bryan,

As a practice I tried writing down your situation.
Here is the code I came up with :


using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;

namespace TestApplication
{
public class TrySerialize
{
public void SerializeAB()
{
A serializeMe = new A();
XmlSerializer x = new XmlSerializer(serializeMe.GetType());
StringWriter stringWriter = new StringWriter();
x.Serialize(stringWriter, serializeMe);
Debug.WriteLine(stringWriter.ToString());
}

}

public class A
{
public string Company = "MyCompany";
public B b = new B();
}

public class B
{
public string Person = "MyName";
}
}


The Serialized result :

<?xml version="1.0" encoding="utf-16"?>
<A xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Company>MyCompany</Company>
<b>
<Person>MyName</Person>
</b>
</A>

Hope this was usefull !
Kind Regards,
 
G

Guest

Hi Rainier,

I've been specifying attributes like <XmlElement(ElementName:="MyElement",
DataType:="string"> and <XmlIngnore> to modify the XML names, hide propertie,
etc. You have accomplished exactly what I'm looking for, but is there an
attribute that I could use for the "public b" variable in Class A to
serialize/deserialize just as you have done? I'm trying to avoid changing
the code in several related programs.

Thanks for your help!
 
G

Guest

I'm not sure I understand the question anymore ?
Looking at the example code you wrote you don't need an attribute at the
place of the question marks to serialize/deserialize class b.
You do need an instance of _classB (_classB = new B()) but thats about it.

public class A
{
public A()
{
_classB = new B();
}
private B _classB;
public string Company = "MyCompany";
public B b
{
get
{
return _classB;
}
set
{
_classB = value;
}
}
}

Bye,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
 
G

Guest

It looks as though I was incorrectly using the XmlElement attribute. I was
attempting to specify the DataType value, when I didn't need to for a class.
I had to have this attributes because the name of the XML differed from the
property name (which I didn't want to change).

Modifying
<XmlElement(ElementName:="MyXMLName", DataType="???")> _
TO
<XmlElement(ElementName:="MyXMLName")> _

Thanks for your help!
 

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