xml反序列化为对象c#

时间:2016-11-03 08:30:48

标签: c# xml xml-deserialization

我在字符串中有xml对象,我想反序列化为C#Object。我测试了简单的性别类,它工作正常,但不确定为什么我的其他xml文档不起作用

XML

<StudentByPersonCode>
  <StudentByPersonCode list="1">
    <Item>
      <PERSON_CODE>33317</PERSON_CODE>
      <FORENAME>Louis</FORENAME>
      <MIDDLE_NAMES>Ane</MIDDLE_NAMES>
      <SURNAME>Sullivan</SURNAME>
      <TITLE>MISS</TITLE>
      <SEX>F</SEX>
      <DATE_OF_BIRTH>1/01/1998 00:00:00</DATE_OF_BIRTH>
      <PROGRESS_STATUS>A</PROGRESS_STATUS>
    </Item>
   </StudentByPersonCode>
 </StudentByPersonCode>

学生班级对象

[Serializable]
[XmlRoot("StudentByPersonCode")]
public class Student
{
    [XmlElement("StudentByPersonCode ")]
    public List<StudentListWrap> StudentListWrap = new List<StudentListWrap>();

    public WebServiceStatus WebServiceStatus { get; set; }
}

public class StudentListWrap
{
    [XmlAttribute("list")]
    public string ListTag { get; set; }

    [XmlElement("Item")]
    public List<Students> Students = new List<Students>();

}


public class Students
{
    [XmlElement("PERSON_CODE")]
    public int PersonCode { get; set; }

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

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

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

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

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

    [XmlElement("DATE_OF_BIRTH")]
    public DateTime DateOfBirth { get; set; }

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

要反序列化的类

 public static T DeserializeXMLToObject<T>(string xml) where T : class
    {
        T obj = null;
        StringReader strReader = null;
        XmlSerializer serializer = null;
        XmlTextReader xmlReader = null;
        try
        {
            strReader = new StringReader(xml);
            serializer = new XmlSerializer(typeof(T));
            xmlReader = new XmlTextReader(strReader);
            obj = (T)serializer.Deserialize(xmlReader);
        }
        catch (Exception exp)
        {
            //Handle Exception Code
            var s = "d";
        }
        finally
        {
            if (xmlReader != null)
            {
                xmlReader.Close();
            }
            if (strReader != null)
            {
                strReader.Close();
            }
        }
        return (T)Convert.ChangeType(obj, typeof(T));
    }

}

运行良好的XML文档

<Genders>
 <Genders list="1">
  <Item>
   <CODE>M</CODE>
   <DESCRIPTION>Male</DESCRIPTION>
  </Item>
  <Item>
   <CODE>F</CODE>
   <DESCRIPTION>Female</DESCRIPTION>
  </Item>
 </Genders>
</Genders>

性别对象类

 [Serializable]
[XmlRoot("Genders")]
public class Gender
{

    [XmlElement("Genders")]
    public List<GenderListWrap> GenderListWrap = new List<GenderListWrap>();

    public WebServiceStatus WebServiceStatus { get; set; }
}


public class GenderListWrap
{
    [XmlAttribute("list")]
    public string ListTag { get; set; }

    [XmlElement("Item")]
    public List<Item> GenderList = new List<Item>();
}



public class Item
{
    [XmlElement("CODE")]
    public string Code { get; set; }

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

2 个答案:

答案 0 :(得分:2)

我建议您在此行中意外添加空格:

[XmlElement("StudentByPersonCode ")]

试试这个:

[XmlElement("StudentByPersonCode")]

答案 1 :(得分:1)

首先,[XmlElement(“StudentByPersonCode”)]中的空格,如@ rbr94所述。

其次,DateTime字段解析不正确。正确的是,例如: <DATE_OF_BIRTH>2011-01-10</DATE_OF_BIRTH>。无论如何,你不需要在出生日期那里的时间。

如果必须从XML解析,日期的正确格式为:

  

2010-01-01T00:00:00