Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 836
prog1011 Avatar asked May 08 '17 05:05

prog1011


People also ask

What is the correct way of using XML Deserialization?

As with the CreatePo method, you must first construct an XmlSerializer, passing the type of class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.

What is deserialize XML?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.

What is XmlSerializer C#?

XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization re-creates the object in its original state from the XML output.


2 Answers

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>
like image 83
John M. Wright Avatar answered Sep 30 '22 14:09

John M. Wright


I have created a sample and the code will be exactly like below. Your model is not correct.

 public class MedicalClearanceForm
{
    [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; }
}
[XmlRoot("MedicalClearanceFormRoot")]
public class MedicalClearanceFormRoot
{


    [XmlElement("MedicalClearanceForm")]
    public MedicalClearanceForm MedicalClearanceForm { get; set; }


}
class Program
{
    static void Main(string[] args)
    {
        string myXMLStringFromDB = @"<MedicalClearanceFormRoot><MedicalClearanceForm PassengerName = 'AAAAAAAAAAAAA' Age = '11' PhoneNo = 'TTTTTTTTTTT' Email = 'ZZZZZZZZZZZZZZZZZZZ' BookingRefNo = '11111111111111111111' /></MedicalClearanceFormRoot >";

        XmlSerializer serializer = new XmlSerializer(typeof(MedicalClearanceFormRoot));
        using (TextReader reader = new StringReader(myXMLStringFromDB))
        {
            MedicalClearanceFormRoot objModel = (MedicalClearanceFormRoot)serializer.Deserialize(reader);
        }

    }
}
like image 39
Kiran B Avatar answered Sep 30 '22 14:09

Kiran B