Deserialize XML to object in C# XmlRoot is not working
Asked Answered
W

3

6

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

Westbrooks answered 8/5, 2017 at 5:18 Comment(2)
The properties are null because <MedicalClearanceFormRoot> doesn't have any attributes declared in the XML. Your hierarchies are off between the XML and the class code.Halutz
Shouldn't MedicalClearanceViewModel class have a MedicalClearanceForm property and MedicalClearanceForm have all the properties currently declared in the MedicalClearanceViewModel class?Turnstone
H
5

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>
Hux answered 8/5, 2017 at 5:45 Comment(0)
R
1

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);
        }

    }
}
Raimundo answered 8/5, 2017 at 5:44 Comment(0)
T
0

To meet the specified XML you would 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; } 
}
Turnstone answered 8/5, 2017 at 5:40 Comment(3)
we can not apply [XmlElement] on class .Westbrooks
@Prog...why not?Turnstone
we can not apply [XmlElement] on class .Colleencollege

© 2022 - 2025 — McMap. All rights reserved.