471,354 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

inherited classes and xml.serialization

suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}

public class C:A
{
public string nameA;

}

and I want to deserialise something like

<?xml?>
<A>
<Name>fred</Name>
<Contents>
<B>
<NameB>bert</NameB>
<Contents>
<A>
<Name>allison</Name>
</Contents>
</A>
<B>
<NameB>jim</NameB>
<Contents>..</Contents>
</Contents>
<A>

In short I want to represent a tree with polymorphic nodes.

I can do this (I think) if I accept that my nodes will read <A
xsi:type="B">, but this will be relatively unreadable. It's possible that I
can do this by overriding IXmlSerializable but I'm not sure if it will handle
deserialization with the inherited types.

I'm sure I can do this by using XmlReader and writing my own parser, but
that's not really the point of this Xml stuff, is it?

Any help or pointers much appreciated!

Iain
May 10 '07 #1
3 4826
Iain wrote:
suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}

public class C:A
{
public string nameA;

}

and I want to deserialise something like

<?xml?>
<A>
<Name>fred</Name>
<Contents>
<B>
<NameB>bert</NameB>
<Contents>
<A>
<Name>allison</Name>
</Contents>
</A>
<B>
<NameB>jim</NameB>
<Contents>..</Contents>
</Contents>
<A>

In short I want to represent a tree with polymorphic nodes.

I can do this (I think) if I accept that my nodes will read <A
xsi:type="B">, but this will be relatively unreadable. It's possible that I
can do this by overriding IXmlSerializable but I'm not sure if it will handle
deserialization with the inherited types.

I'm sure I can do this by using XmlReader and writing my own parser, but
that's not really the point of this Xml stuff, is it?

Any help or pointers much appreciated!

Iain
Hi, Iain
If the inherited type is serializable, there wouldn't be problems. Well
the XmlSerializer has certain limitations, but we don't live in a
perfect world ;)

There's a pretty good article at
http://www.codeproject.com/useritems...Serializer.asp

HTH,
Petar Atanasov
http://a-wake.net
May 11 '07 #2


"Petar Atanasov" wrote:
Iain wrote:
suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}
....

Thanks for your note, Petar. It turns out that the answer was (as you said)
quite straightforward. A good nights sleep and some code tests found it.

As the code above stands it does not serialise the B class as a B but as a
<B xsi:type="B">

Equally if I have a <Bin some text xml it gets ignored by the
deserialisation.

However, if I change the A class definition to

public class A
{
[XmlInclude(typeof(A))]
[XmlInclude(typeof(B))]
List<AContents;
}

public class B
{
....
}

It just plain works!

Iain

May 11 '07 #3
Iain wrote:
>
"Petar Atanasov" wrote:
>Iain wrote:
>>suppose I have

public class A
{
public List<AContents;
}

public class B: A
{
public string nameB;

}
...

Thanks for your note, Petar. It turns out that the answer was (as you said)
quite straightforward. A good nights sleep and some code tests found it.

As the code above stands it does not serialise the B class as a B but as a
<B xsi:type="B">

Equally if I have a <Bin some text xml it gets ignored by the
deserialisation.

However, if I change the A class definition to

public class A
{
[XmlInclude(typeof(A))]
[XmlInclude(typeof(B))]
List<AContents;
}

public class B
{
...
}

It just plain works!

Iain
yeah :) good sleep always helps ;)

Regards,
Petar Atanasov
http://a-wake.net
May 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Glenn Wilson | last post: by
2 posts views Thread by tony lock | last post: by
12 posts views Thread by Michael Maes | last post: by
reply views Thread by cnSoftware | last post: by
4 posts views Thread by Mihai | last post: by
8 posts views Thread by chr | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.