.net - Xml serialization of child class with dotnet. Don't write the child root node from parent class -


here's problem. have parent class "foo" , child class "bar" :

[serializable] public class foo, ixmlserializable {      public bar child {get; set;}           #region ixmlserializable membres      public system.xml.schema.xmlschema getschema()      {             return null;     }     public void readxml(system.xml.xmlreader reader)     {         throw new notimplementedexception();     }      public void writexml(system.xml.xmlwriter writer)      {         new xmlserializer(this.child.gettype()).serialize(writer, this.child);      }         #endregion }   [serializable] public class bar {     [xmlelement]     public string myelement1 {get; set;}     [xmlelement]     public string myelement2 {get; set;} } 

if serialize classes it, i'am going :

<xml> <foo>     <bar>         <myelement1>beer</myelement>         <myelement2>vodka</myelement>     </bar> </foo> 

but, how can control serialization from "foo" (parent) class remove "bar" node. want have :

<xml> <foo>     <myelement1>beer</myelement>     <myelement2>vodka</myelement> </foo> 

this sample volontary simple. help!

you need change foo's writexml method , this:

public void writexml(system.xml.xmlwriter writer) {    //new xmlserializer(this.child.gettype()).serialize(writer, this.child);    writer.writeelementstring("myelement1", this.child.myelement1);     writer.writeelementstring("myelement2", this.child.myelement2); } 

this render out xml you're looking (basically make <bar> node vanish).


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -