XML反序列化到对象c#

本文关键字:对象 反序列化 XML | 更新日期: 2023-09-27 18:16:10

我有一个字符串xml对象,我想反序列化为c#对象。我测试了简单的性别类,它工作得很好,但不确定为什么我的其他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; }
}

XML反序列化到对象c#

我建议您不小心在这一行添加了一个空格:

[XmlElement("StudentByPersonCode ")]

试试这个:

[XmlElement("StudentByPersonCode")]

首先是[XmlElement("StudentByPersonCode ")]中的空白,正如@rbr94所提到的。

第二,DateTime字段解析不正确。正确的是,例如:<DATE_OF_BIRTH>2011-01-10</DATE_OF_BIRTH>。不管怎样,你不需要出生日期的小时。

如果要从XML解析日期,正确的日期格式是:

2010 - 01 - 01 t00:00:00