.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Code Snippets >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Function to convert datatable data into XML

Posted By:Rajshree       Posted Date: October 25, 2010    Points: 15    Category: XML    URL: http://www.dotnetspark.com  

Here is the function which convert datatable data into XML where Columns in datatable are treated as Attribute in XML

protected string ConvertDataTableToXML(DataTable tableToBeConverted_, string tableName_)
            //Change the column mapping first.
            DataTable tbl = new DataTable();
            tbl = tableToBeConverted_.Copy();
            tbl.TableName = tableName_;

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            //Column maping is attribute than column value will be attribute of row node
            foreach (DataColumn col in tbl.Columns)
                col.ColumnMapping = System.Data.MappingType.Attribute;
            tbl.WriteXml(sw, System.Data.XmlWriteMode.IgnoreSchema);

            return sb.ToString();


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend