c# - How to add xsi:type attribute to an XML element -
c# - How to add xsi:type attribute to an XML element -
how can add together xsi:type in xml element.
i writing routine in c# serializing xml file using xmlserializer. seems fine except first thought minor turned out not so.
here code,
public class outerelement { public string firstelement { get; set; } public string secondelement { get; set; } public innerelement innerelement = new innerelement(); } public class innerelement { [xmlattribute(attributename="xsi:type")] public string type { get; set; } } private void form1_load(object sender, eventargs e) { xmlserializer serializer = new xmlserializer(typeof(outerelement)); outerelement outerelement = new outerelement(); outerelement.firstelement = "name"; outerelement.secondelement = "cd"; outerelement.innerelement.type = "testsample"; using (textwriter author = new streamwriter(@"g:\abc.xml")) { serializer.serialize(writer, outerelement); } } '
i want xml this,
<outerelement xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" lns:xsd="http://www.w3.org/2001/xmlschema"> <innerelement xsi:type="testsample"> </innerelement> <firstelement>name</firstelement> <secondelement>cd</secondelement> </outerelement> thanks in advance.
you need declare right namespace attribute, so:
public class innerelement { [xmlattribute(namespace = "http://www.w3.org/2001/xmlschema-instance")] public string type = "bla"; } this produce desired output:
<outerelement xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <innerelement xsi:type="testsample" /> <firstelement>name</firstelement> <secondelement>cd</secondelement> </outerelement> c# xml xml-serialization
Comments
Post a Comment