Search code examples
c#xml-serialization

Serializing Class having property of type object


The Property Value of Type object can be single value of any type like int, string, decimal etc. Or it can be a collection of values like List < int >, List < string > etc.

public class Criteria
{
    [XmlElement("IDS")]
    public object Value
    {
        get; set;
    }
}

Current Output:

<CriteriaGroup>
  <Criteria p2:type="Criteria" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
    <IDS p2:type="ArrayOfInt">
      <int>2610</int>
      <int>2452</int>
    </IDS>
  </Criteria>
  <Criteria p2:type="Criteria" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
    <IDS p2:type="ArrayOfString">
      <string>CUMULU1MO</string>
      <string>ALLIEDWX2</string>      
    </IDS>
  </Criteria>
</CriteriaGroup>

Expected Output:

How can I achieve below result? Tried decorating public object Value with XmlArray, XmlArrayItem but no luck.

<CriteriaGroup>
  <Criteria>
    <IDS>
      <ID>2610</ID>
      <ID>2452</ID>
    </IDS>
  </Criteria>
  <Criteria>
    <IDS>
      <ID>CUMULU1MO</ID>
      <ID>ALLIEDWX2</ID>      
    </IDS>
  </Criteria>
</CriteriaGroup>

Used below method to Serialize.

public static string Serialize<T>(T data)
{
    string xmlData = string.Empty;
    XmlSerializer ser = new XmlSerializer(typeof(T));
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");
    
    var settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.OmitXmlDeclaration = true;
    
    using (StringWriter sw = new StringWriter())
    {
        using (XmlWriter xw = XmlWriter.Create(sw, settings))
        {
            ser.Serialize(xw, data, ns);
            xmlData = sw.ToString();
        }
    
    }
    return xmlData;
}

Solution

  • Try following :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                CriteriaGroup CriteriaGroup = new CriteriaGroup()
                {
                    Criteria = new List<Criteria>() {
                        new Criteria() { Value = new string[] { "2610","2452"}
                        },
                        new Criteria() { Value = new string[] {"CUMULU1MO", "ALLIEDWX2"}
                        }
                    }
                };
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(FILENAME, settings);
                XmlSerializer serializer = new XmlSerializer(typeof(CriteriaGroup));
                serializer.Serialize(writer, CriteriaGroup);
            }
        }
        public class CriteriaGroup
        {
            [XmlElement()]
            public List<Criteria> Criteria { get; set; }
        }
        public class Criteria
        {
            [XmlArray("IDS")]
            [XmlArrayItem("ID")]
            public string[] Value
            {
                get;
                set;
            }
        }
    }