简体   繁体   中英

xml deserialize to object c#

I have xml object in string which I want to deserialize to C# Object. I have test simple gender class and it worked fine but not sure why my other xml doc is not work

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>

Student Class Object

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

Class to deserialize

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

}

The XML doc that work fine

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

Gender Object class

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

I propose that you accidently added a space in this line:

[XmlElement("StudentByPersonCode ")]

Try this:

[XmlElement("StudentByPersonCode")]

First of all, the whitespace in [XmlElement("StudentByPersonCode ")], as @rbr94 has mentioned.

Second, the DateTime field is parsing incorrectly. The correct one would be, for example : <DATE_OF_BIRTH>2011-01-10</DATE_OF_BIRTH> . You don't need the hour there anyway for the date of birth.

The correct format for the date if it has to be parsed from XML would be:

2010-01-01T00:00:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM