1

I found out how to convert a DataTable into XML using this post - How can I convert a DataTable to an XML file in C#?

string result;
using (StringWriter sw = new StringWriter()) {
dataTable.WriteXml(sw);
result = sw.ToString();
}

In the above code, we see that each row will be enclosed with TABLE tags. Is there any way I could replace it with custom tags, like ROW ? Of course, I could search the string itself and replace TABLE with ROW, but I want a different approach.

Community
  • 1
  • 1
Steam
  • 9,368
  • 27
  • 83
  • 122
  • You could write an XSLT Transform and run it against the resulting XML. – John Saunders Dec 20 '13 at 06:47
  • @JohnSaunders - Conceptually speaking, what does that mean ? – Steam Dec 20 '13 at 18:15
  • 1
    It's not conceptual. XML Transforms (XSLT) are an XML language which describe how to transform one XML document into another, or into HTML or text. They are implemented in .NET as the [XslCompiledTransform class](http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx). – John Saunders Dec 20 '13 at 18:18

1 Answers1

2

you can use Linq to traverse all the rows of DataTable and build the string you want

Reading values from a DataTable column into a List<string> with LINQ

Or you can use LinqToXml to change the XML returned from WriteXml method

Linq to XML - update/alter the nodes of an XML Document

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153