xml - c# serialize object without root element -


i working 3rd party has xml structure multiple different requests.

each request has common header structure , specific footer data.

unfortunately, header , footer sections not wrapped within own element tags , can't change.

some sample contrived requests shown below:

sample request 1

<?xml version="1.0" encoding="utf - 8" standalone="yes"?> <request>     <requesttype>1</requesttype>     <user>user01</user>     <id>1234</id>     <name>john</name>     <age>20</age> </request> 

sample request 2

<?xml version="1.0" encoding="utf - 8" standalone="yes"?> <request>     <requesttype>2</requesttype>     <user>user02</user>     <id>1235</id>     <school>the school</school>     <teacher>mrs smith</teacher> </request> 

sample request 3

<?xml version="1.0" encoding="utf - 8" standalone="yes"?> <request>     <requesttype>3</requesttype>     <user>user01</user>     <id>223</id>     <work>the office</word>     <boss>mr white</boss>     <phone>1234567</phone>     <payday>friday</payday> </request> 

you can see each request has requesttype, user , id.

my question relates writing c# code encapsulate xml serialization.

to me, seems wrong have each of c# classes having repeated header (requesttype, user , id) data.

i have tried using generics (see sample code below) leads question.

question: how can serialize generic object footer not wrapped within "root" footer element?

[system.xml.serialization.xmlroot("request")] public class genericrequest<typet> {     public genericrequest()     {     }      public int requesttype { get; set; }      public string user { get; set; }      public int id { get; set; }      public typet footer { get; set; } } 

you can create class interface ifooter, can have different implementor per requirement (e.g.

interface ifooter : iserializable  {  //define common member. }  public class footer1: ifooter {  // define members (e.g. work)  }  public class footer2: ifooter {  // define members  }  public class footer3: ifooter {  // define members  } 

now serialize main class

[system.xml.serialization.xmlroot("request")] public class genericrequest {   public genericrequest()   {   }     public int requesttype { get; set; }    public string user { get; set; }    public int id { get; set; }    public ifooter footer { get; set; } } 

visit how can serialize object has interface property? reference.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -