Below is the my XML file.
<Employee>
<FirstName>#{FirstName}#</FirstName>
<LastName>#{LastName}#</LastName>
<DOB>#{DOB}#</DOB>
<Address>#{Address}#</Address>
<Transcation>
<Month>#{Month}#</Month>
<Amount>#{Amount}#</Amount>
</Transcation>
<Transcation>
<Month>#{Month}#</Month>
<Amount>#{Amount}#</Amount>
</Transcation>
<Transcation>
<Month>#{Month}#</Month>
<Amount>#{Amount}#</Amount>
</Transcation>
<Transcation>
<Month>#{Month}#</Month>
<Amount>#{Amount}#</Amount>
</Transcation>
and my serializable class is
[XmlRoot("Employee"), Serializable]
public class Employee
{
[XmlAnyElement]
public List<XmlElement> EmployeeDetails { get; set; }
}
But i want to get something like this
In my EmployeeDetails i should serialize only FirstName, LastName, DOB, Address,…. and I should get List of Transcation in a separate class which contains Month and Amount as serialiazable element.
something like this
[XmlRoot("Employee"), Serializable]
public class Employee
{
[XmlAnyElement]
public List<XmlElement> EmployeeDetails { get; set; }
[XmlElement("Transcation")]
public List<Transcation> Transcations { get; set; }
}
public class Transcation
{
[XmlElement("Month")]
public string Month{ get; set; }
[XmlElement("Amount")]
public string Amount{ get; set; }
}
How can i do this ?
Assuming you can’t modify the XML your class should look like the following. XmlAnyElement can be used however you will probably need to set it as an object array. So your code should look like this:
To Deserialize you do the following:
I recommend instead to specify the properties, it will make it easier to look for specific values. But this depends on what you know about the xml schema before hand.