2

我想将以下 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; }
 }

技能数组有2个技能,反序列化后数组只有1个技能,组和属性数组为空...

我做错了什么?

4

2 回答 2

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

我建议您通过属性尽可能多地向 XMLSerializer 提供信息。你似乎并没有太离谱。使用上述定义,我能够成功反序列化您提供的 XML。

于 2012-08-20T10:54:00.830 回答
2

首先定义一个类技能,然后在你的合同类中使用这个类。

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

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

于 2012-08-20T10:52:46.313 回答