IXmlSerializable and XmlAttribute - How can I make it work like DateTime does?

582 views Asked by At

I have a class that has a fairly simple string representation. Serializing and deserializing to and from this string is not difficult.

What I would like to be able to do is use an XmlAttribute like so:

public class MyClass
{

     [XmlAttribute("MyValue")]
     public MySubClass {get; set;}

}

So that MySubClass, whenever it's serialized, can appear as an attribute:

<Myclass MyValue="XXYYZZ">
// other stuff omitted
</MyClass>

I'm currently attempting to use IXmlSerializable in MySubClass, but I get this serialization exception:

XmlAttribute/XmlText cannot be used to encode types implementing IXmlSerializable."

I know this is possible because it works with DateTime. I've looked at the source code for DateTime and I see that ISerializable is implemented, but thats for binary serialization. I also see IFormattable implemented, but I don't know if that's the trick.

Question: how can I make a class that behaves just like DateTime when it comes to xml serialization within xml attributes?

0

There are 0 answers