I want to convert a DataTable to an XML file in C#. How can I do this?
相关问题
- Sorting 3 numbers without branching [closed]
- Illegal to have multiple roots (start tag in epilo
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
You can use the
writeXML
method to save it as XML (Source).You can also use serialization/desirialization as described in the fifth post of this forum.
You can use DataTable.WriteXml Method.
Here is an example;
How can i convert my datatable into XML using C# 2.0?
If you don't actually need a string but read-only, processable XML, it's a better idea to use MemoryStream and XPathDocument:
Another way to get this done is by adding the data table to dataset and calling the
GetXml()
on the dataset.In addition to this dataset is equipped with theWriteXml()
andReadXml()
for writing/reading the XML directly to/from a file path or stream.