使用XmlSerializer将xml元素反序列化为Array并不起作用

时间:2012-08-20 10:13:22

标签: c# .net deserialization xmlserializer

我想将以下xml反序列化到我的类中,我无法更改xml,因为它通过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>

我的课程:

 [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; }
 }

技能的arry有2个技能,反序列化后arry中只有1个技能,group和properties数组为null ...

我做错了什么?

2 个答案:

答案 0 :(得分:6)

  1. 您应该使用XmlArray和XmlArrayItem属性正确装饰数组属性。对于例如对于技能属性,您使用XmlElement与XmlArray是不允许的。

    [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. 确保xmlArrayItem'types'具有正确的读/写属性

    public class Skill
    {
        public string Name
        {
            get;
            set;
        }
        public string Mandatory
        {
            get;
            set;
        }
    }
    
  3. 我建议您尽可能通过属性向XMLSerializer提供尽可能多的信息。 你似乎没有太过分。使用上面的定义,我能够成功地反序列化您提供的XML。

答案 1 :(得分:2)

首先定义一个Skill类,然后在Contract类中使用该类。

// 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; }
}

请为群组和属性执行相同的操作。