user559399
user559399

Reputation: 3

xml deserialization, tag formatted multiple ways

Attempting to deserialize some xml, but the third party I'm retrieving it from has formatted a specific tag in multiple ways. In the example below it is the value tag. It can either have a string or multiple internal tags with more information

Preferably I'd like this to happen automatically and continue following this kind of code structure

[XmlRoot(ElementName = "ContactField")]
public class ContactField
{
    [XmlElement("id")]
    public int id { get; set; }

    [XmlElement("type")]
    public string type { get; set; }

    [XmlElement("value")]
    public FieldValue values { get; set; }

}

Is there any way to use XmlSerializer to handle the tag differently based on its contents or am I going to be stuck doing it manually?

<contact>
        <isConnection>false</isConnection>
        <id>62</id>
        <fields>
            <id>8</id>
            <type>id</type>
            <value>[email protected]</value>
            <editedBy>OWNER</editedBy>
        </fields>
        <fields>
            <id>95</id>
            <type>notes</type>
            <value>Dummy user for testing</value>
            <editedBy>OWNER</editedBy>
        </fields>
        <fields>
            <id>96</id>
            <type>birthday</type>
            <value>
                <day>11</day>
                <month>5</month>
                <year>1988</year>
            </value>
            <editedBy>OWNER</editedBy>
        </fields>
</contact>

Upvotes: 0

Views: 79

Answers (1)

dan
dan

Reputation: 198

I think you can achieve it by defining your classes something like this

   [XmlRoot(ElementName = "contact")]
   public class ContactField
        {
            [XmlElement("id")]
            public int id { get; set; }

            [XmlElement("type")]
            public string type { get; set; }

            [XmlElement("fields")]
            public Field[] fields { get; set; }
        }

        public class Field
        {
            [XmlElement("value")]
            public SubField SubField { get; set; }
        }

        public class SubField
        {
            [XmlText]
            public String Value { get; set; }

            [XmlElement("day")]
            public String Day { get; set; }

            [XmlElement("month")]
            public String Month { get; set; }

            [XmlElement("year")]
            public String Year { get; set; }
        }

I tested it using like this

    String xml = @"<contact>
        <isConnection>false</isConnection>
        <id>62</id>
        <fields>
            <id>8</id>
            <type>id</type>
            <value>[email protected]</value>
            <editedBy>OWNER</editedBy>
        </fields>
        <fields>
            <id>95</id>
            <type>notes</type>
            <value>Dummy user for testing</value>
            <editedBy>OWNER</editedBy>
        </fields>
        <fields>
            <id>96</id>
            <type>birthday</type>
                <value><day>11</day>
                <month>5</month>
                <year>1988</year></value>
            <editedBy>OWNER</editedBy>
        </fields>
</contact>";
var xmlSerializer  = new XmlSerializer(typeof (ContactField));
   using (var stringReader = new StringReader(xml))
   {
        var contactField = (ContactField) xmlSerializer.Deserialize(stringReader);
   }

Upvotes: 2

Related Questions