serialization - How to serialize container classes using custom serializer in C# -


i need serialize container classes hasvalue property evaluated true.

i don´t need remove invalid elements container list prior serialization. serializer should able determine objects need serialized or not. guess custom serializer can suitable need don´t know how figure out this. other solution / best practice appreciated.

here classes

public static class containerfactory {          public static container create()          {              var container = new container();              container.persons.addrange(new[]                  {                      new person                          {                              firstname = "thomas"                          },                       new person                          {                              firstname = "andrew",                              lastname = "martin",                               vehicles = new vehicles                                  {                                     new vehicle { hsn = "65976ghr", tsn = "huzukl"}                                  }                          },                           new person                          {                              firstname = "arnold",                              lastname = "beckmann",                              vehicles = new vehicles                                  {                                     new vehicle { hsn = "345xxxhz"},                                     new vehicle { hsn = "659juki", tsn = "787999hgf"}                                  }                          }                   });              return container;          } }  [serializable] public class container {     public container()     {         persons = new persons();     }      public persons persons { get; set; }      public void serialize()     {         var serializer = new xmlserializer(typeof (container));         var streamwriter = new streamwriter(@"c:\container.xml", false);         serializer.serialize(streamwriter, this);     } }  public class persons: list<person> { }  public class vehicles: list<vehicle> {     public vehicles()     {     }      public vehicles(ienumerable<vehicle> vehicles):base(vehicles)     {     } }  [serializable] public class person : ihasvalue {     public person()     {         this.vehicles = new vehicles();         this.id = guid.newguid().tostring();     }      public string id { get; set; }     public string firstname { get; set; }     public string lastname { get; set; }     public vehicles vehicles { get; set; }      public bool hasvalue     {         { return !string.isnullorempty(this.firstname) && !string.isnullorempty(this.lastname); }     } }  public interface ihasvalue {     bool hasvalue { get;} }  public class vehicle: ihasvalue {     public string hsn { get; set; }     public string tsn { get; set; }      public bool hasvalue     {         { return !string.isnullorempty(hsn) && !string.isnullorempty(tsn); }     } }  //using .net xmlserializer test container container container = containerfactory.create(); container.serialize(); console.writeline("press key continue..."); console.readline();  

output

<?xml version="1.0" encoding="utf-8"?> <container xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">   <persons>     <person>       <id>76cdcc18-b256-40fe-b813-cd6c60e682ca</id>       <firstname>thomas</firstname>       <vehicles />     </person>     <person>       <id>26623bf9-d799-44d2-bc1a-7ec91292d1cd</id>       <firstname>andrew</firstname>       <lastname>martin</lastname>       <vehicles>         <vehicle>           <hsn>65976ghr</hsn>           <tsn>huzukl</tsn>         </vehicle>       </vehicles>     </person>     <person>       <id>f645cde1-10c8-4df5-81df-9b9db7712ec3</id>       <firstname>arnold</firstname>       <lastname>beckmann</lastname>       <vehicles>         <vehicle>           <hsn>345xxxhz</hsn>         </vehicle>         <vehicle>           <hsn>659juki</hsn>           <tsn>787999hgf</tsn>         </vehicle>       </vehicles>     </person>   </persons> 

how can achieve goal serialize vehicles/persons hasvalue == true?

you need use:

  1. iserializable interface - every class want serialize , every property want in different node need use appropriate attribute. @ example here
  2. when serialize class check if hasvalue == true

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -