D
Daniel Ulfe
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
2="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![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Any ideas or pointers would be appreciate it.
Thanks!
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
![Stick Out Tongue :p :p](/styles/default/custom/smilies/tongue.gif)
</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
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
Any ideas or pointers would be appreciate it.
Thanks!