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
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.
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.
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.
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>
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With