开发者

Change-proof and fastest way to transform a DataTable to XML?

I have a (dynamically created) DataTable and I'd like to convert it to XML, with (possibly) very different structure. The user will choose which column represent the "properties" that the transformation will use. For example

|Column 1|Column 2| Column 3|

Resulting XML#1:

<root>
  <node id="Column 1">
  <anothernode id="Column1" target="Column2">
    <data>Column 3</sata>
  </anothernode>
</root>

Or even this structure:

<root>
  <nodes>
    <node label="Column 1">
      <data>Column 3</data>
 开发者_JAVA百科 </nodes>
</root>

And so on.

Question: how can do this in the fastest way possible, allowing future changes (read: add new xml way of transforming the data table)? Qhat I need is just a few tips, like a general "design pattern".

Please: no XMLTransform involved.


Have you tried DataTable.WriteXml ??

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.


You can use the XmlWriter class to create an xml according to the structure you desire. You can optimize the code further if you know in advance the columns the data table will contain.

private void WriteDataTableToXml(DataTable dataTable, string xmlFilePath)
{   
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "  ";
    settings.NewLineOnAttributes = true;

    using (StreamWriter streamWriter = new StreamWriter(xmlFilePath, false, Encoding.GetEncoding("ISO-8859-1")))
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, settings))
        {
            xmlWriter.WriteStartElement("root");
            xmlWriter.WriteStartElement("nodes");

            foreach (DataRow dataRow in dataTable.Rows)
            {
                xmlWriter.WriteStartElement("node");

                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    xmlWriter.WriteElementString(dataColumn.ColumnName, dataRow[dataColumn].ToString());
                }

                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndElement();
        }
    }
}

The call to this method will be like this:

string xmlFilePath = @"D:\test.xml";

DataTable dataTable = new DataTable();
dataTable.Columns.Add("Col1");
dataTable.Columns.Add("Col2");
dataTable.Columns.Add("Col3");

dataTable.Rows.Add(1, "abc", "zyx");
dataTable.Rows.Add(2, "cde", "mno");
dataTable.Rows.Add(3, "def", "fru");

WriteDataTableToXml(dataTable, xmlFilePath);

And the xml created will be as follows:

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
  <nodes>
    <node>
      <Col1>1</Col1>
      <Col2>abc</Col2>
      <Col3>zyx</Col3>
    </node>
    <node>
      <Col1>2</Col1>
      <Col2>cde</Col2>
      <Col3>mno</Col3>
    </node>
    <node>
      <Col1>3</Col1>
      <Col2>def</Col2>
      <Col3>fru</Col3>
    </node>
  </nodes>
</root>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜