我在C#Visual Studio 2010中有以下代码。
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Database\\db.mde"; string sql = "SELECT * FROM Customer"; OleDbConnection connection = new OleDbConnection(connectionString); OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); adapter.Fill(ds, "Test Table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Test Table"; ds.WriteXml("C:\\Users\\Desktop\\testfile.XML");现在它做了我想要的一切,但我需要在导出时稍微修改XML文件的格式。 这可以轻松完成吗? 我收集我需要提供一个很好的模式文件,但我不知道如何使用数据集实现它。
目前,XML看起来像这样。
<?xml version="1.0" standalone="yes"?> <NewDataSet> <Test_x0020_Table> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Test_x0020_Table> </NewDataSet>......但我希望它看起来像这样
<?xml version="1.0" standalone="yes"?> <Customers> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customers>任何帮助将不胜感激。
I have the following code in C# Visual Studio 2010.
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Database\\db.mde"; string sql = "SELECT * FROM Customer"; OleDbConnection connection = new OleDbConnection(connectionString); OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); adapter.Fill(ds, "Test Table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Test Table"; ds.WriteXml("C:\\Users\\Desktop\\testfile.XML");Now it does everything I want but I need to slightly modify the format of the XML file when it exports. Can this be easily done? I gather I need to provide a schema file which is fine but I'm not sure how to implement it with the dataset.
Currently the XML looks like this.
<?xml version="1.0" standalone="yes"?> <NewDataSet> <Test_x0020_Table> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Test_x0020_Table> </NewDataSet>... but I want it to look like this
<?xml version="1.0" standalone="yes"?> <Customers> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customers>Any help would be greatly appreciated.
最满意答案
第一个节点是表的名称。
您可以通过在DataSet上设置名称来更改此设置。
DataSet ds = new DataSet(“ Customers ”); //复数
内部节点是记录。
您可以通过在填充适配器时指定名称来更改此设置。
adapter.Fill(ds,“ Customer ”); //单数
这会给你一个结果,如:
<Customers> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> </Customers>这意味着如果您返回多个结果,最终会得到:
<Customers> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> </Customers>您不能拥有单个“客户”,因为在您编写XML时,它会将其格式化为“表格”和“记录”。
The first node is the name of your table.
You can change this by setting a name on your DataSet.
DataSet ds = new DataSet("Customers"); //plural
The inner node is the record.
You can change this by specifying the name when you fill the adapter.
adapter.Fill(ds, "Customer"); //singular
This will give you a result like:
<Customers> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> </Customers>This means if you return multiple results you would end up with:
<Customers> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> <Customer> <name>Customer1</name> <address>25 Big St</address> <suburb>Sydney NSW</suburb> <contact>Fred Nurk</contact> <phone>11 1111 1111</phone> </Customer> </Customers>You can't have a single "Customer" because when you write the XML it's formatting it as "Table" with "Records".
更多推荐
XML,string,电脑培训,计算机培训,IT培训"/> <meta name="description"
发布评论