Converting a Data object into an XML file just by using a Data object and XMLSerializer,
The Data class
This is the data class. which will be used to convert a data object into an xml string .
Required references
The Method Call
This method call simply takes the data object and convert the object content into a XML string.
The converting method
This method converts the data object into an XML string
This is the data class. which will be used to convert a data object into an xml string .
public class ExampleDataClass
{
public string Type { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public List ExampleElements { get; set; }
}
System.Xml.Serialization;
This method call simply takes the data object and convert the object content into a XML string.
ExampleDataClass dataObject = new ExampleDataClass ();
...Set data to object ...
string xmlString = ToXMLString(dataObject);
This method converts the data object into an XML string
public static string ToXMLString(object item)
{
var stringwriter = new System.IO.StringWriter();
var serializer = new XmlSerializer(item.GetType());
serializer.Serialize(stringwriter, item);
return stringwriter.ToString();
}
Latest blogs
Created: 13/01/2017 Total Comment: 0