C# XmlSerializer ignores xs:attribute with types other than xs:string -
my problem seems odd , did not find other problem, guess simple , stupid mistake, can't seem find out.
i have xsd, generate class structure using xsd.exe. "fill" object values, when serializing xml ignores class-properties not of type string
.
var mygraph = new graph(); mygraph.mystring = "hallo"; mygraph.myint = 80; var serializer = new xmlserializer(typeof(graph)); textwriter writefilestream = new streamwriter(path.combine(outfolder, outfile)); serializer.serialize(writefilestream, mygraph); writefilestream.close();
i expected:
<graph xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" mystring="hallo" myint="80" />
the actual output is:
<graph xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" mystring="hallo" />
the attribute myint
has been ignored. if define string, appear, other type not show up. if declare required
, leave null
, serialized myint="0"
.
what missing?
some details:
xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="graph"> <xs:complextype> <xs:attribute name="mystring" type="xs:string" /> <xs:attribute name="myint" type="xs:int" /> </xs:complextype> </xs:element> </xs:schema>
generated class:
[system.codedom.compiler.generatedcodeattribute("xsd", "2.0.50727.3038")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(anonymoustype=false)] [system.xml.serialization.xmlrootattribute(namespace="", isnullable=false)] public partial class graph { private string mystringfield; private int myintfield; [system.xml.serialization.xmlattributeattribute(form=system.xml.schema.xmlschemaform.qualified)] public string mystring { { return this.mystringfield; } set { this.mystringfield = value; } } [system.xml.serialization.xmlattributeattribute(form=system.xml.schema.xmlschemaform.qualified)] public int myint { { return this.myintfield; } set { this.myintfield = value; } } [system.xml.serialization.xmlignoreattribute()] public bool myintspecified { { return this.myintfieldspecified; } set { this.myintfieldspecified = value; } }
xsd add additional "specified" field properties value types. when using .net serialization value types, need specify field's value , set matching "specified" property true.
change code , work expected:
var mygraph = new graph(); mygraph.mystring = "hallo"; mygraph.myint = 80; mygraph.myintspecified = true;
Comments
Post a Comment