Serialize XML from C# class, need to set namespaces -
i'm having trouble getting top-level element using xmlserializer (and c# attributes):
<rootobject xmlns="http://www.example.com/xmlschemas/nonstandardschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.example.com/xmlschemas/nonstandardschema1.xsd"> <otherserializedobjects /> </rootobject>
currently, closest i've gotten this:
<rootobject xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" d1p1:schemalocation="http://www.example.com/xmlschemas/nonstandardschema1.xsd" xmlns:d1p1="xsi" xmlns="http://www.example.com/xmlschemas/nonstandardschema"> <otherserializedobjects /> </rootobject>
c#
[xmlroot("rootobject", namespace = "http://www.example.com/xmlschemas/nonstandardschema")] public class rootobject { [xmlattribute("schemalocation", namespace = "xsi")] public string schemalocation { get; set; } [xmlelement("image")] public object[] otherserializedobjects { get; set; } public rootobject() { otherserializedobjects = new object[]{}; } } public class program { static void main(string[] args) { var rootobject = new rootobject { schemalocation = "http://www.example.com/xmlschemas/nonstandardschema1.xsd", otherserializedobjects = new object[]{} }; var serializer = new xmlserializer(typeof(rootobject)); var stringwriter = new stringwriter(); serializer.serialize(stringwriter, rootobject); console.writeline(stringwriter.tostring()); } }
any ideas?
firstly, [xmlattribute(namespace=x)]
attribute on schemalocation
field/property needs have full namespace value of x
, not local namespace shortcut. incidentally, can property rather field. using field purpose wastes memory.
secondly, eliminate standard xmlns:xsd="http://www.w3.org/2001/xmlschema"
, use xmlserializer.serialize
overload pass in xmlserializernamespaces
namespaces want.
thus:
[xmlroot("rootobject", namespace = "http://www.example.com/xmlschemas/nonstandardschema")] public class rootobject { public static xmlserializernamespaces getadditionalnamespaces() { xmlserializernamespaces xsns = new xmlserializernamespaces(); xsns.add("", "http://www.example.com/xmlschemas/nonstandardschema"); xsns.add("xsi", "http://www.w3.org/2001/xmlschema-instance"); return xsns; } [xmlattribute("schemalocation", namespace = "http://www.w3.org/2001/xmlschema-instance")] public string xsdschemalocation { { return "http://www.example.com/xmlschemas/nonstandardschema1.xsd"; } set { // nothing - fake property. } } [xmlelement("image")] public object[] otherserializedobjects { get; set; } public rootobject() { otherserializedobjects = new object[]{}; } }
and use like:
var rootobject = new rootobject { otherserializedobjects = new object[]{} }; var serializer = new xmlserializer(typeof(rootobject)); var stringwriter = new stringwriter(); var ns = rootobject.getadditionalnamespaces(); var settings = new xmlwritersettings() { indent = true, indentchars = " " }; // cosmetic purposes. using (var xmlwriter = xmlwriter.create(stringwriter, settings)) { serializer.serialize(xmlwriter, rootobject, ns); } console.writeline(stringwriter.tostring());
example fiddle.
Comments
Post a Comment