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?

reynaerta
  • 45
  • 1
  • 1
  • 7
  • Why don't you check this question http://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml and use tools suggested in answers? – Oleg Aug 20 '12 at 10:50

2 Answers2

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.

Amit Mittal
  • 2,646
  • 16
  • 24
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.

bala_88
  • 391
  • 1
  • 3
  • 14