Search code examples
c#asp.netxmlasp.net-mvcxml-deserialization

Deserialize XML to object in C# XmlRoot is not working


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


Solution

  • The way your XML is defined, you would need to have two objects defined: - one for the MedicalClearanceFormRoot xml node - one for the MedicalClearanceForm xml node

    So, you have two routes you could take: add the wrapper class or change your xml.

    To add a wrapper class, you would need to have a class to represent MedicalClearanceFormRoot which has a property for the MedicalClearanceForm object. Then change your serializer class to be for the wrapper class. Here's an example:

    [XmlRoot("MedicalClearanceFormRoot")]
    public class Wrapper
    {
        public MedicalClearanceViewModel MedicalClearanceForm { get; set;}
    }
    
    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; }
    }
    
    
            XmlSerializer serializer = new XmlSerializer(typeof(Wrapper));
            using (TextReader reader = new StringReader(myXMLStringFromDB))
            {
                Wrapper objModel = (Wrapper)serializer.Deserialize(reader);
            }
    

    Option 2: Change your XML to look like this:

    <MedicalClearanceFormRoot PassengerName="AAAAAAAAAAAAA" Age="11" PhoneNo="TTTTTTTTTTT" Email="ZZZZZZZZZZZZZZZZZZZ" BookingRefNo="11111111111111111111" >  
    </MedicalClearanceFormRoot>