Tuesday, 11 November 2014

Convert XML Values to the Custom object in C#

 string strXML = System.IO.File.ReadAllText(@"Path...\Desktop\test.xml");
            var customObjectXml = new CustomObject();
            XmlSerializer oXmlSerializer = new XmlSerializer(customObjectXml.GetType());
            //The StringReader will be the stream holder for the existing XML file
            customObjectXml = (CustomObject)oXmlSerializer.Deserialize(new StringReader(strXML));

This is the simple code we can use to convert the xml related values to Custom .NET object.
This code is getting the XML from the path and converting to Custom Object by using the XMLSerializer and the Deserializer.  If it is not working properly we have to compare the XML values and custom object XmlRoot and XmlType

[XmlRoot("NewGroupName")]
[XmlType("NewTypeName")]
[XmlAttribute("href")]