dotnet xml:
Jimmy,
I know what what you're asking for, but unfortunately this functionality is
not available in the .NET framework.
You can achieve the first functionality (selective serialization) with
functionality that's built into the serialization framework to support the
omitting value types from the serialized document.
[XmlIgnore]
public bool MyFieldSpecified;
private int _MyField;
public int MyField
{
get
{
return _MyField;
}
set
{
if( value == 0 )
{
MyFieldSpecified = false;
}
else
{
MyFieldSpecified = true;
}
_MyField = value;
}
}
You can solve the other scenarios you mentioned with properties that perform
where the accessors perform the format conversions, with decorator-like
wrapper classes, or by doing everything by hand and implementing
IXmlSerialziable. Unfortunately there is no easy declarative way to solve
this. Keep asking for it though. Maybe it helps if others are asking for it,
too ;)
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
[quoted text, click to view] "Jimmy Seow" <wea@cawe.com> wrote in message
news:%23dM8VUMLEHA.1612@TK2MSFTNGP12.phx.gbl...
> Dear All,
> Hope somebody can help me with this.
>
> I have created a class and have got it to serialize and deserialize using
> XmlSerialization without any problems. I've also read the documentation on
> the XML attribute classes such as XmlElement and XmlRoot etc as well as
> XmlAttributeOverrides.
>
> However, I run into a problem if I need to perform a conversion on a
> property before serializing it. For example, if I have a class with a
> property called SomeProperty with a type of Integer. If SomeProperty's
value
> is 0, I don't want to serialize it but if it's not zero, I want to
serialize
> it.
>
> Or, another example would be, if I have a class with a property of integer
> and I need to serialize the output to XML to convert the value to a
string.
> ie SomeProperty = 5 and I need the XML output to give me a string "Five"
> instead of the default conversion value of "5".
>
> My thinking is that there should be an easy way of applying an attribute
to
> the property that translates the output of the property to XML and
> vice-versa. It would be great if there were an attribute called
> XMLConvertAttribute that will specify the conversion to be performed to
XML.
> But I can't find any information on this. Is there any other easy way?
>
> I know that I could always customize my XML serialization by using
> Surrogates or by using IXMLSerializable interface or by just using the
quick
> and dirty way, by using a function to read all the values in the class and
> build the XML by hand. However, all these solutions forces me to customize
> serialization for the entire class while all I need is to perform custom
> conversion on a couple of properties. Also, I wanted a good
object-oriented
> method and I just thought somewhere somehow, somebody must have a neat
> solution to this problem.
>
> Thanks a lot for any help.
>
>