Converting a Data class object content into an XML string
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 .

public class ExampleDataClass
{
public string Type { get; set; }
public string Code { get; set; }
public string Description { get; set; }

public List ExampleElements { get; set; }
}



Required references

System.Xml.Serialization;



The Method Call
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);



The converting method
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();
}
Created: 13/01/2017 Total Comment: 0

Comments

(Not Displayed)
Human validation
Enter 9354 number, before submitting to confirm your human
[Home] [All Blogs] [Contact] [Me]