使用DataSet.WriteXml创建XML。如何更改节点名称? [英] Creating XML using DataSet.WriteXml. How to change the node name?

查看:533
本文介绍了使用DataSet.WriteXml创建XML。如何更改节点名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变 DataSet 默认节点名?我从 DataTable 创建XML。

Is there any way to change the DataSet default node name? I am creating XML from a DataTable.

这是我的代码:

DataSet dataSet = new DataSet("Products");
dataSet.Tables.Add(tbl);
dataSet.WriteXml(@"D:\Temp\test.xml");

这是我得到的XML:

 <Products>
    <Table1>
      <product_name>McWilliams Hanwood Chardonnay 750mL</product_name>      
      <id>121385</id>
      <price>7.60</price>
    </Table1>
    <Table1>
        ...
    </Table1>
    <Table1>
        ...
    </Table1>
</Products>

是否有办法将 Table1 更改为产品 like:

Is there a way to change Table1 to Product like:

<Products>
 <Product>
 ....
 </Product>
  <Product>
 ....
 </Product>
</Products>


推荐答案

出现的名称是 DataTable 。您可以使用 TableName 属性更改其名称,如下所示:

The name that appears is the name of the DataTable. You can change its name using the TableName property like this:

DataSet dataSet = new DataSet("Products");
tbl.TableName = "Product";
dataSet.Tables.Add(tbl);
dataSet.WriteXml(@"D:\Temp\test.xml");

您也可以将Product构造表时的名称:

You can also pass the "Product" name when you construct the table:

var tbl = new DataTable("Product");

这篇关于使用DataSet.WriteXml创建XML。如何更改节点名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆