From: Daniel Ulfe on
Hello,

I have been reading a lot of pages and I cannot seem to find a easy
solution for my problem...

I have to deserialize (and serialize) a file which looks like this:

**************
<record>
<field1>data1</field1>
<field2 nil="true"></field2>
</record>
**************


That is it... no XML header, no namespace, no comments, etc... I
created a class like this:


**************
public class record()
{
public string field1 {get; set; }
[XmlElement(IsNullable=true)]
public int? field2 {get; set; }
}
**************


and tried to serialize it with:


**************
myinstance = new record();
myinstance.field1="data1";

XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration
= true };
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer ser = new XmlSerializer(myinstance.GetType());
using (StringWriter sw = new StringWriter())
{
XmlWriter writer = XmlWriter.Create(sw, settings);
ser.Serialize(writer, myinstance, ns);
}
**************


But that renders it as:


**************
<record>
<field1>data1</field1>
<fiel2 p2:nil="true"
xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
</record>
**************


I have also tried decorating field2 using [XmlElement(IsNullable-true,
Namespace="")] but that did not work either.

I know that I can do it by using the IXmlSerializable interface... that
I have multiple classes that require that behavior... so I prefer not to
"manually" serialize all those classes.


I thought about creating my own XML Serializer... but that will not use
any optimizations the "regular" XMLSerializer does... and it will
require a lot of code too :)

Any ideas or pointers would be appreciate it.

Thanks!
From: Peter Duniho on
Daniel Ulfe wrote:
> [...]
> I know that I can do it by using the IXmlSerializable interface... that
> I have multiple classes that require that behavior... so I prefer not to
> "manually" serialize all those classes.

In general, if you don't want to write custom serialization, you need to
accept the data format imposed on you. If you can't accept the data
format imposed on you, you need to write some kind of custom serialization.

> I thought about creating my own XML Serializer... but that will not use
> any optimizations the "regular" XMLSerializer does... and it will
> require a lot of code too :)

It may be worthwhile if you expect to have to have a large number of
types that comply with your specific format. The System.Xml.Linq
namespace provides a very nice DOM paradigm for dealing with the XML
itself, and the reflection side of things is probably not too bad as
compared to having to implement IXmlSerializable in every single type.

Pete
From: Daniel Ulfe on
Yeah... I guess my best bet would be creating a custom serializer....

Thank you Pete!
Daniel.

On 3/9/2010 11:02 PM, Peter Duniho wrote:
> Daniel Ulfe wrote:
>> [...]
>> I know that I can do it by using the IXmlSerializable interface...
>> that I have multiple classes that require that behavior... so I prefer
>> not to "manually" serialize all those classes.
>
> In general, if you don't want to write custom serialization, you need to
> accept the data format imposed on you. If you can't accept the data
> format imposed on you, you need to write some kind of custom serialization.
>
>> I thought about creating my own XML Serializer... but that will not
>> use any optimizations the "regular" XMLSerializer does... and it will
>> require a lot of code too :)
>
> It may be worthwhile if you expect to have to have a large number of
> types that comply with your specific format. The System.Xml.Linq
> namespace provides a very nice DOM paradigm for dealing with the XML
> itself, and the reflection side of things is probably not too bad as
> compared to having to implement IXmlSerializable in every single type.
>
> Pete