In this post we will see the simple approach to deserialize XML to C# object.
Our XML String
1 | string xmlString = "<Students><Student><Id>1</Id><Name>Manish Dubey</Name></Student><Student><Id>2</Id><Name>Pankaj</Name></Student></Students>" ; |
Create our class
1 2 3 4 5 | public class Student { public int Id { get ; set ; } public string Name { get ; set ; } } |
Create XML Serializer and StringReader Object
1 2 3 4 | //First argument is type of object and second argument is root attribute of your XML string/source XmlSerializer serializer = new XmlSerializer( typeof (List<student>), new XmlRootAttribute( "Students" )); StringReader stringReader = new StringReader(xmlString); |
At last, deserialize XMl to C# object
1 | List<student> studentList = (List<student>)serializer.Deserialize(stringReader); |
Output
![]() |
Add caption |
No comments:
Post a Comment