I am using asp.net (C#) website, I want to Deserialize
the XML
string using XmlSerializer
class.
My Model (.cs file)
[XmlRoot("MedicalClearanceFormRoot")]
public class MedicalClearanceViewModel
{
[XmlAttribute("PassengerName")]
public string PassengerName { get; set; }
[XmlAttribute("Gender")]
public string Gender { get; set; }
[XmlAttribute("Age")]
public string Age { get; set; }
[XmlAttribute("PhoneNo")]
public string PhoneNo { get; set; }
[XmlAttribute("Email")]
public string Email { get; set; }
[XmlAttribute("BookingRefNo")]
public string BookingRefNo { get; set; }
}
XML String
<MedicalClearanceFormRoot>
<MedicalClearanceForm PassengerName="AAAAAAAAAAAAA" Age="11" PhoneNo="TTTTTTTTTTT" Email="ZZZZZZZZZZZZZZZZZZZ" BookingRefNo="11111111111111111111" />
</MedicalClearanceFormRoot>
Code to De-Serialize the XML to Object
string myXMLStringFromDB = GetXMLStringFromDb(); // this method will return XML from db.
XmlSerializer serializer = new XmlSerializer(typeof(MedicalClearanceViewModel));
using (TextReader reader = new StringReader(myXMLStringFromDB))
{
MedicalClearanceViewModel objModel = (MedicalClearanceViewModel)serializer.Deserialize(reader);
}
But, the issue is when I De-serialize the XML to object using above code ... the properties like PassengerName
, Age
, PhoneNo
Etc. are still blank in objModel
Can someone can help me to set the proper XML Notations on my Class on can someone can help me to resolve this issue .
Any help will be highly appreciated ! Thanks
<MedicalClearanceFormRoot>
doesn't have any attributes declared in the XML. Your hierarchies are off between the XML and the class code. – Halutz