将 DataTable 转换为 XML 的防更改且最快的方法?

发布于 2024-10-06 08:07:56 字数 697 浏览 5 评论 0原文

我有一个(动态创建的)DataTable,我想将其转换为 XML,其结构(可能)非常不同。用户将选择哪一列代表转换将使用的“属性”。例如,

|Column 1|Column 2| Column 3|

结果 XML#1:

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

甚至是这个结构:

<root>
  <nodes>
    <node label="Column 1">
      <data>Column 3</data>
  </nodes>
</root>

等等。

问题:如何以尽可能最快的方式做到这一点,并允许将来进行更改(阅读:添加新的 xml 方式来转换数据表)?我需要的 Qhat 只是一些技巧,就像一般的“设计模式”。

:不涉及 XMLTransform。

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>
  </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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-10-13 08:07:56

您是否尝试过 DataTable.WriteXml

WriteXml 方法提供了一种将 DataTable 中的数据仅写入 XML 文档或同时将数据和架构写入 XML 文档的方法,而 WriteXmlSchema 方法仅写入架构。要写入数据和架构,请使用包含 XmlWriteMode 参数的重载之一,并将其​​值设置为 WriteSchema。

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.

秋日私语 2024-10-13 08:07:56

您可以使用 XmlWriter 类根据您想要的结构创建 xml。如果您事先知道数据表将包含哪些列,则可以进一步优化代码。

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();
        }
    }
}

对该方法的调用如下:

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);

创建的 xml 如下:

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文