Convert to XML

Ever needed the xml file from a DataTable or DataSet?
With this class at the bases you will get started

Lets start with the class

public static class Xml
{
    /// 
    /// Create xml from a DataTable
    ///
    /// This extension will not use the Schema from the DataTable,
    /// for this reason it is compatible with openXML
    /// 
    /// 
    /// Returns xml based on the given DataTable without its schema
    public static string prepareXML(this System.Data.DataTable dataTable)
    {
        StringBuilder sb = new StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        dataTable.WriteXml(sw, System.Data.XmlWriteMode.IgnoreSchema);
        return sb.ToString();
    }

    /// 
    /// Create xml from a DataSet
    ///
    /// This extension will not use the Schema from the DataSet,
    /// for this reason it is compatible with openXML
    /// 
    /// 
    /// Returns xml based on the given DataSet without its schema
    public static string prepareXML(this System.Data.DataSet dataSet)
    {
        StringBuilder sb = new StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        dataSet.WriteXml(sw, System.Data.XmlWriteMode.IgnoreSchema);
        return sb.ToString();
    }
}

Now how to use it

DataTable dataTable = getDataTable();
string xml = dataTable.prepareXML();

Simple but handy


Warning: count(): Parameter must be an array or an object that implements Countable in /home/p20382/domains/2be-it.com/public_html/programming/wordpress/wp-includes/class-wp-comment-query.php on line 405

Leave a Reply