toxic toxic - 1 month ago 8
C# Question

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

Answer

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

[XmlElement("StudentByPersonCode ")]

Try this:

[XmlElement("StudentByPersonCode")]
Comments