Parse XML string to class in C#? [duplicate] Parse XML string to class in C#? [duplicate] xml xml

Parse XML string to class in C#? [duplicate]


You can just use XML serialization to create an instance of the class from the XML:

XmlSerializer serializer = new XmlSerializer(typeof(Book));using (StringReader reader = new StringReader(xmlDocumentText)){    Book book = (Book)(serializer.Deserialize(reader));}


There are several ways to deserialize an XML document - the XmlSerializer living in System.Xml.Serialization and the newer DataContractSerializer which is in System.Runtime.Serialization.

Both require that you decorate your class members with attributes that tell the serializer how to operate (different attributes for each).