Remove XML descriptor to object in C # XmlRoot doesn't work
I am using asp.net site (C #), I want a Deserialize
string XML
using class XmlSerializer
.
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 an XML 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 problem is that I de-serializes the XML-object using the above code ... properties such as PassengerName
, Age
, PhoneNo
and etc. are still empty inobjModel
Can anyone help me set the correct XML notation in my class, can someone please help me solve this problem.
Any help would be much appreciated! thank
source to share
As your XML is defined, you will need to define two objects: - one for the MedicalClearanceFormRoot
xml node - one for the MedicalClearanceForm
xml node
So you have two routes you can take, add wrapper class or change your xml.
To add a wrapper class, you will need to have a view class MedicalClearanceFormRoot
that has a property for the object MedicalClearanceForm
. Then change the serializer class to a 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. Modify your XML like this:
<MedicalClearanceFormRoot PassengerName="AAAAAAAAAAAAA" Age="11" PhoneNo="TTTTTTTTTTT" Email="ZZZZZZZZZZZZZZZZZZZ" BookingRefNo="11111111111111111111" >
</MedicalClearanceFormRoot>
source to share
To match the specified XML you will need this class structure, I believe.
[XmlRoot("MedicalClearanceFormRoot")]
public class MedicalClearanceViewModel
{
public MedicalClearanceFormElement MedicalClearanceForm { get; set; }
}
[XmlElement]
public class MedicalClearanceFormElement
{
[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; }
}
source to share
I have created a sample and the code will be exactly as shown below. Your model is wrong.
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);
}
}
}
source to share