2

I want to deserialize the following xml into my class, i can't change the xml because it commes from a device over tcp/ip.

<?xml version="1.0" encoding="utf-8"?>
<CONTACTINFORMATION UID="1234">
 <LoginId><![CDATA[1234]]></LoginId>
<ContactId><![CDATA[2134]]></ContactId>
<ContactType>CCININTERN</ContactType>
<Status>CONVERSATION</Status>
<From><![CDATA[123]]></From>
<To><![CDATA[123]]></To>
<WaitTime><![CDATA[123]]></WaitTime>

<ContactPropertySummary>
  <ContactProperty>
    <Name><![CDATA[13]]></Name>
    <Value><![CDATA[13]]></Value>
    <Hidden>NO</Hidden>
    <Url><![CDATA[13]]></Url>
  </ContactProperty>
</ContactPropertySummary>

<SkillSummary>
  <Skill>
    <Name><![CDATA[123]]></Name>
    <Mandatory>YES</Mandatory>
  </Skill>

  <Skill>
    <Name><![CDATA[124]]></Name>
    <Mandatory>YES</Mandatory>
  </Skill>
</SkillSummary>

<ContactCodeSummary>
  <ContactCode>
    <Id>123</Id>
    <Hidden>NO</Hidden>
    <Assigned>YES</Assigned>
  </ContactCode>

</ContactCodeSummary>
<GroupSummary>
  <Group>
    <Name><![CDATA[123]]></Name>
    <Mandatory>YES</Mandatory>
  </Group>

</GroupSummary>
<PreviousAgent><![CDATA[2]]></PreviousAgent>
<ScratchPadId><![CDATA[2]]></ScratchPadId>
<ScratchPadData><![CDATA[2]]></ScratchPadData>
<FaxSpecific>
  <NbrOfPages>2</NbrOfPages>
</FaxSpecific>
 </CONTACTINFORMATION>

My class:

 [Serializable]
 [XmlRoot("CONTACTINFORMATION")]
 public class Contact
  {
    #region :: PROPERTIES ::
    public string LoginId { get; set; }
    public string ContactId { get; set; }
    public ContactType ContactType { get; set; }
    public ContactStatus Status { get; set; }
    [XmlElement("From")]
    public string ContactFrom { get; set; }
    [XmlElement("To")]
    public string ContactTo { get; set; }
    public int WaitTime { get; set; }

    [XmlElement("SkillSummary", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlArray("Skill")]
    //[XmlElement("SkillSummary",  Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public Skill[] Skills { get; set; }


    [XmlArray("ContactPropertySummary")]
    public ContactProperty[] Properties { get; set; }


    [XmlArray("GroupSummary", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlArrayItem("Group", typeof(Group), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public Group[] Groups { get; set; }
 }

The arry of skills has 2 skills, after deserializing there is only 1 skill in the arry, the groups and properties array is null...

What i'm doing wrong?

1

2 Answers 2

6
  1. You should properly decorate array properties with XmlArray and XmlArrayItem attributes. For e.g. for skills property you are using XmlElement with XmlArray which is not permitted.

    [XmlArrayItem("Skill", typeof(Skill), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlArray("SkillSummary", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public Skill[] Skills
    {
      get;
      set;
    }
    
    [XmlArray("ContactPropertySummary")]
    [XmlArrayItem("ContactProperty", typeof(ContactProperty), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ContactProperty[] Properties
    {
      get;
      set;
    }
    
    [XmlArray("GroupSummary", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlArrayItem("Group", typeof(Group), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public Group[] Groups
    {
      get;
      set;
    }
    
  2. Make Sure that xmlArrayItem 'types' have proper read/write properties

    public class Skill
    {
        public string Name
        {
            get;
            set;
        }
        public string Mandatory
        {
            get;
            set;
        }
    }
    

I recommend you to provide as much information to the XMLSerializer, through attributes, as you can. You don't seem to be too off the mark. Using the above definitions I was able to successfully deserialize the XML you provided.

Sign up to request clarification or add additional context in comments.

Comments

2

Start by defining a class Skill and then using this class in your Contract class.

// We're going to define a class called Skill
[Serializable()]
public class Skill
{
    [System.Xml.Serialization.XmlElement("Name")]
    public string Name { get; set; }

    [System.Xml.Serialization.XmlElement("Mandatory")]
    public string Mandatory { get; set; }
}

[Serializable]
[XmlRoot("CONTACTINFORMATION")]
public class Contact
{
   // ...... Rest of your elements
   [XmlArray("SkillSummary")]
   [XmlArrayItem("Skill", typeof(Skill))]
   public Skills[] Skill { get; set; }
}

Please do the same for Groups and Properties as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.