c# - How to serialize multiple objects into an existing XmlDocument, without having the namespaces on each component? -
how serialize multiple objects existing xmldocument in .net/c#?
i have xmldocument, contains data. have multiple objects. want serialize them 1 1 , add together them xmldocument (appendchild).
this how should be:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <project> <mysettings>...</mysettings> <component_1> anydata </component_1> ... <component_x> anydata </component_x> </project>
when utilize xmlserializer definition each component:
<component_1 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> anydata </component_1>
so get, when serialize string , create xmlnode string, append document:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <project> <mysettings>...</mysettings> <component_1 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> anydata </component_1> ... <component_x xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> anydata </component_x> </project>
i can remove namespace doing this:
xmlserializernamespaces ns = new xmlserializernamespaces(); ns.add("", ""); stringwriter xout = new stringwriter(); x.serialize(xout, data, ns);
but namespaces on object within object array. object:
public class component_1 { object[] arr; }
will serialized to:
<component_1> <objectarray> <anytype xmlns:q1="http://www.w3.org/2001/xmlschema" d3p1:type="q1:string" xmlns:d3p1="http://www.w3.org/2001/xmlschema-instance">one</anytype> <anytype xmlns:q2="http://www.w3.org/2001/xmlschema" d3p1:type="q2:string" xmlns:d3p1="http://www.w3.org/2001/xmlschema-instance">two</anytype> </objectarray> </component_1>
is possible add together needed namespaces, etc. document , serialize objects xmlnodes , add together them document, without having namespaces on each component?
update: function test() serialize 2 objects , append them document. lastly line deserialize first object.
using system; using system.io; using system.text; using system.xml; using system.xml.serialization; ... public class component_1 { public string value = "component_1.value"; public object[] objectarray = new object[] { "one", "two" }; } void test() { object[] components = new object[] { new component_1(), new component_1() }; xmldocument doc = new xmldocument(); xmlnode rootnode = doc.appendchild(doc.createelement("project")); foreach (var component in components) rootnode.appendchild(doc.readnode(xmltextreader.create(new stringreader(serialize(component, true))))); console.writeline(doc.outerxml); console.writeline(deserialize<component_1>(rootnode.childnodes[0].outerxml).value); } string serialize(object obj, bool namespaces) { stringbuilder sb = new stringbuilder(); xmlwriter author = xmlwriter.create(sb, new xmlwritersettings() { omitxmldeclaration = true }); (new xmlserializer(obj.gettype())).serialize(writer, obj, namespaces ? null : new xmlserializernamespaces(new xmlqualifiedname[] { new xmlqualifiedname("", "") })); homecoming sb.tostring(); } t deserialize<t>(string xmlstring) { homecoming (t)(new xmlserializer(typeof(t))).deserialize(new stringreader(xmlstring)); }
maybe possible add together namespaces document (rootnode) , when creating new xmlnode string function xmldocument.readnode resolve namespaces in string namespaces xmldocument. don´t know how.
update 2: alex filipovici, serialization output exact wanted.
void test2() { object[] components = new object[] { new component_1(), new component_1() }; var doc = new xmldocument(); var project = doc.appendchild(doc.createelement("project")); doc.documentelement.setattribute("xmlns:xsi", "http://www.w3.org/2001/xmlschema-instance"); doc.documentelement.setattribute("xmlns:xsd", "http://www.w3.org/2001/xmlschema"); var nav = project.createnavigator(); var emptynamepsaces = new xmlserializernamespaces(new[] { xmlqualifiedname.empty }); foreach (var component in components) { using (var author = nav.appendchild()) { var serializer = new xmlserializer(component.gettype()); writer.writewhitespace(""); serializer.serialize(writer, component , emptynamepsaces ); writer.close(); } } foreach (xmlnode node in doc.getelementsbytagname("anytype")) { string attributetype = ""; foreach (xmlattribute xmlattribute in node.attributes) { if (xmlattribute.localname == "type") { attributetype = xmlattribute.value.split(':')[1]; } } node.attributes.removeall(); node.createnavigator().createattribute("", "type", "", attributetype); } doc.save("output.xml"); component_1 c = deserialize<component_1>(project.childnodes[0].outerxml); console.writeline(c.objectarray[0].gettype()); // -> system.xml.xmlnode[] ! }
output:
<project xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <component_1> <value>component_1.value</value> <objectarray> <anytype type="string">one</anytype> <anytype type="string">two</anytype> </objectarray> </component_1> <component_1> <value>component_1.value</value> <objectarray> <anytype type="string">one</anytype> <anytype type="string">two</anytype> </objectarray> </component_1> </project>
but deserialization "t desirialize(string xmlstring)" function above fails. object array contains xmlnodes.
is possible tell xmlserializer utilize namespaces project node, or have insert them again?
this serialize objects , append them xmldocument. while de-/serializing code resolve namespaces. @alex: illustration xpathnavigator.
void test2() { xmldocument doc = new xmldocument(); xmlnode root = doc.appendchild(doc.createelement("root")); doc.documentelement.setattribute("xmlns:xsi", "http://www.w3.org/2001/xmlschema-instance"); doc.documentelement.setattribute("xmlns:xsd", "http://www.w3.org/2001/xmlschema"); serializeappend(root, new object[] { 1, "two", 3.0 }); // serialize object , append xmlnode var obj = deserialize<object[]>(root.childnodes[0]); // deserialize xmlnode object } t deserialize<t>(xmlnode node) { xpathnavigator nav = node.createnavigator(); using (var reader = nav.readsubtree()) { var serializer = new xmlserializer(typeof(t)); homecoming (t)serializer.deserialize(reader); } } void serializeappend(xmlnode parentnode, object obj) { xpathnavigator nav = parentnode.createnavigator(); using (var author = nav.appendchild()) { var serializer = new xmlserializer(obj.gettype()); writer.writewhitespace(""); serializer.serialize(writer, obj); writer.close(); } }
c# .net xml-serialization
No comments:
Post a Comment