XML : XML parse to class in C#

I created a generic handler that receives XMLs,

I have two XML formats I could receive:

1st :

  <?xml version="1.0" encoding="UTF-8"?>  <message>  <ms type="test">  <description>  <add>  <num type="1">1234</num>  </add>  </description>  <src>  <add>  <num type="2">111</num>  </add>  </src>  <sis type="1" encoding="0">Lorem Ipsum</sis>  </ms>  </message>    

2nd:

  <?xml version="1.0" encoding="UTF-8"?>  <message>  <ms type="test">  <description>  <add>  <num type="1">111</num>  </add>  </description>  <rsd type="33"/>  <sis type="1" encoding="0">Lorem Ipsum</sis>  </ms>  </message>    

Both XMLs are similar, with few elements more or less.

I am using the parser that maps XML to the Class.

Which means The names in the class have to be exactly the same as in the XML.

I cant create 2 classes called "message", and when I tried merging the two XMLs into one class, I got a parsing error.

Here's the class after I merged them :

  /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]  public partial class message  {        private messageMS msField;        /// <remarks/>      public messageMS ms      {          get          {              return this.msField;          }          set          {              this.msField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMS  {        private messageMSDescription descriptionField;        private messageMSSrc srcField;        private messageMSRsd rsdField;        private messageMSSis sisField;        private string typeField;        /// <remarks/>      public messageMSDescription description      {          get          {              return this.descriptionField;          }          set          {              this.descriptionField = value;          }      }        /// <remarks/>      public messageMSSrc src      {          get          {              return this.srcField;          }          set          {              this.srcField = value;          }      }        /// <remarks/>      public messageMSRsd rsd      {          get          {              return this.rsdField;          }          set          {              this.rsdField = value;          }      }        /// <remarks/>      public messageMSSis sis      {          get          {              return this.sisField;          }          set          {              this.sisField = value;          }      }        /// <remarks/>      [System.Xml.Serialization.XmlAttributeAttribute()]      public string type      {          get          {              return this.typeField;          }          set          {              this.typeField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSDescription  {        private messageMSDescriptionAdd addField;        /// <remarks/>      public messageMSDescriptionAdd add      {          get          {              return this.addField;          }          set          {              this.addField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSDescriptionAdd  {        private messageMSDescriptionAddNum numField;        /// <remarks/>      public messageMSDescriptionAddNum num      {          get          {              return this.numField;          }          set          {              this.numField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSDescriptionAddNum  {        private byte typeField;        private ushort valueField;        /// <remarks/>      [System.Xml.Serialization.XmlAttributeAttribute()]      public byte type      {          get          {              return this.typeField;          }          set          {              this.typeField = value;          }      }        /// <remarks/>      [System.Xml.Serialization.XmlTextAttribute()]      public ushort Value      {          get          {              return this.valueField;          }          set          {              this.valueField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSSrc  {        private messageMSSrcAdd addField;        /// <remarks/>      public messageMSSrcAdd add      {          get          {              return this.addField;          }          set          {              this.addField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSSrcAdd  {        private messageMSSrcAddNum numField;        /// <remarks/>      public messageMSSrcAddNum num      {          get          {              return this.numField;          }          set          {              this.numField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSSrcAddNum  {        private byte typeField;        private byte valueField;        /// <remarks/>      [System.Xml.Serialization.XmlAttributeAttribute()]      public byte type      {          get          {              return this.typeField;          }          set          {              this.typeField = value;          }      }        /// <remarks/>      [System.Xml.Serialization.XmlTextAttribute()]      public byte Value      {          get          {              return this.valueField;          }          set          {              this.valueField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSRsd  {        private byte typeField;        /// <remarks/>      [System.Xml.Serialization.XmlAttributeAttribute()]      public byte type      {          get          {              return this.typeField;          }          set          {              this.typeField = value;          }      }  }    /// <remarks/>  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]  public partial class messageMSSis  {        private byte typeField;        private byte encodingField;        private string valueField;        /// <remarks/>      [System.Xml.Serialization.XmlAttributeAttribute()]      public byte type      {          get          {              return this.typeField;          }          set          {              this.typeField = value;          }      }        /// <remarks/>      [System.Xml.Serialization.XmlAttributeAttribute()]      public byte encoding      {          get          {              return this.encodingField;          }          set          {              this.encodingField = value;          }      }        /// <remarks/>      [System.Xml.Serialization.XmlTextAttribute()]      public string Value      {          get          {              return this.valueField;          }          set          {              this.valueField = value;          }      }  }    

Anyone got an alternative solution on how to handle this?

No comments:

Post a Comment