471,075 Members | 737 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Problem with IXmlSerializable, inheritance, and xsi:type

I am having trouble using IXmlSerializable within a base class and
having xsi:type work correctly when deserializing an xml file. Here is
a very simplified version of the what is going on in the code:

public class BaseJunkClass : IXmlSerializable
{
public void ReadXml(XmlReader Reader) { }

public void WriteXml(XmlWriter Writer) { }

public XmlSchema GetSchema()
{
return null;
}
}

public class DerivedJunkClass : BaseJunkClass
{
}

public class JunkClassContainer
{
public BaseJunkClass JunkClass;
}

And here is the code to deserialize this:

Type[] JunkTypes = new Type[] { typeof(BaseJunkClass),
typeof(DerivedJunkClass) };

XmlSerializer JunkSerializer = new
XmlSerializer(typeof(JunkClassContainer), JunkTypes);

FileStream JunkStream = new FileStream("JunkConfiguration.xml",
FileMode.Open, FileAccess.Read);

JunkClassContainer TestSerialization =
(JunkClassContainer)JunkSerializer.Deserialize(Jun kStream);

And here are the xml file contents:

<?xml version="1.0" encoding="utf-8" ?>
<JunkClassContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance">

<JunkClass xsi:type="DerivedJunkClass"></JunkClass>

</JunkClassContainer>

After running this code, TestSerialization contains an instance of
type BaseJunkClass, instead of DerivedJunkClass. What's more strange,
is that when this same code runs on the compact framework, it works as
desired. That is, in the compact framework TestSerialization contains
an instance of type DerivedJunkClass.

I came up with a dirty solution to fix this, but I was wondering if
there was a cleaner way. Maybe I'm missing a SerializableAttribute
somewhere, or something additional in the xml file? Any thought or
suggestions would be appreciated.

Thanks.

Apr 24 '07 #1
0 1531

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by tim.almond | last post: by
reply views Thread by frugalprogrammer | last post: by
1 post views Thread by luthriaajay | last post: by
reply views Thread by =?Utf-8?B?U2hhbm5vbiBCYXJiZXI=?= | last post: by
2 posts views Thread by oh.i.love.spam | last post: by
reply views Thread by leo001 | 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.