1

I am currently working on a WebService and struck at how to define properties class for the below use-case:

<Parent>
<Type1>int</Type1>
<Type2>string</Type2>
<Type3>Object</Type3>
<Type3>Object</Type3>
<Type3>Object</Type3>
<Type3>Object</Type3>
</Parent>

My code looks like this:

[XmlRoot]
class Parent
{
   [XmlElement]
   public int Type1 {get; set;}

   [XmlElement]
   public string Type2 {get; set;}

   [XmlArray]
   public List<Type3ListItem> Type3 {get; set;}
}

public class Type3ListItem
{
   public string ABC {get; set;}
   public string DEF {get; set;}
}

And I would like to Deserialize using System.Xml.Serialization, please advise on how to proceed with this use-case. Also advise if using System.Runtime.Serialization.Formatters.Binary; is better than System.Xml.Serialization for better performance to consume XML of size 50MB(approx).

dbc
  • 104,963
  • 20
  • 228
  • 340
Nagaraja Malla
  • 144
  • 1
  • 10
  • Have you tried to put [XmlInclude(typeof(Type3ListItem))] above Parent Class? – Jawad Zeb Apr 11 '18 at 08:54
  • @JawadZeb Thank you for quick reply, no I haven't tried that. I will give it a try. – Nagaraja Malla Apr 11 '18 at 08:56
  • 1
    Yes dear you can follow this example: https://stackoverflow.com/questions/14326832/how-to-make-a-serializable-class-that-contains-an-instance-of-one-class-from-a-s – Jawad Zeb Apr 11 '18 at 08:58
  • Because you also have a list of Type3ListItem In the parent. so the link I pasted is the same that you want to achieve :https://stackoverflow.com/questions/14326832/how-to-make-a-serializable-class-that-contains-an-instance-of-one-class-from-a-s – Jawad Zeb Apr 11 '18 at 08:59
  • Are you looking for [XML Serialization - Disable rendering root element of array](https://stackoverflow.com/q/2006482/3744182)? If so the answer there explains you just need to apply `[XmlElement("Type3")]` to `Type3` in place of `[XmlArray]`. – dbc Apr 22 '18 at 18:01

0 Answers0