首页 新闻 搜索 专区 学院

dataset导出数据 合并xml

1
悬赏园豆:80 [已解决问题] 解决于 2012-02-16 14:46
导出数据库的为xml文件,用dataset读取Customer表数据,和本地的orders.xml 合并,然后组成一个xml文件。可是orders一直是orders.xml的数据,合并后还是orders.xml的数据,Customer 的数据已经读取出来了。在合并时,orders.xml 怎么一直不变。我希望2个不同数据结构的表,在导出时 一个表作为xml的大节点,表的列名在大节点下,然后接着另外一个表。

SqlDataAdapter adapter = new SqlDataAdapter("select * from [Customer]", conn);

  DataSet customers = new DataSet();
  adapter.FillSchema(customers, SchemaType.Source, "Customer");
  adapter.Fill(customers, "Customer");

  DataSet orders = new DataSet();
  orders.ReadXml("D:\\orders.xml");
   

  orders.Merge(customers,true);
flover7的主页 flover7 | 初学一级 | 园豆:84
提问于:2012-02-15 15:26
< >
分享
最佳答案
0
#region 将数据导出对应的表xml
SqlDataAdapter daTable = new SqlDataAdapter("select name from sysobjects where xtype='U' order by name asc", conn);
DataTable dTable = new DataTable();
daTable.Fill(dTable);
DataSet dsTable = new DataSet();
dsTable.Tables.Add(dTable);

DataSet dsAll = new DataSet();


DataSet dsxml = new DataSet();
dsxml.WriteXml("D:\\ds1.xml");
for (int i = 0; i < dsTable.Tables[0].Rows.Count; i++)
{

string sqlTable = dsTable.Tables[0].Rows[i][0].ToString();
string fileName = "D:\\Project" + "\\" + "ExprotToXml" + "\\" + "XML" + "\\" + sqlTable + ".xml";
string sql = "select * from " + sqlTable + "";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt1 = new DataTable(sqlTable);
sda.Fill(dt1);
DataSet ds1 = new DataSet();
ds1.Tables.Add(dt1);
//ds1.WriteXml(fileName);
DataSet ds2 = new DataSet();
ds2.ReadXml("D:\\ds1.xml");
ds2.Merge(ds1);
ds2.WriteXml("D:\\ds1.xml");
}

//FileStream fs = new FileStream("D:\\123.xml", FileMode.Create);
//StreamWriter writer = new StreamWriter(fs);
//writer.Write(ds);
//writer.Close();
//fs.Close();



#endregion
flover7 | 初学一级 |园豆:84 | 2012-02-16 14:44
其他回答(1)
0

xml转dataset需要先读取

/**////<summary>
/// XML形式的字符串、XML文江转换成DataSet、DataTable格式
///</summary>
public class XmlToData
{
/**////<summary>
/// 将Xml内容字符串转换成DataSet对象
///</summary>
///<param name="xmlStr">Xml内容字符串</param>
///<returns>DataSet对象</returns>
public static DataSet CXmlToDataSet(string xmlStr)
{
if (!string.IsNullOrEmpty(xmlStr))
{
StringReader StrStream = null;
XmlTextReader Xmlrdr = null;
try
{
DataSet ds = new DataSet();
//读取字符串中的信息
StrStream = new StringReader(xmlStr);
//获取StrStream中的数据
Xmlrdr = new XmlTextReader(StrStream);
//ds获取Xmlrdr中的数据
ds.ReadXml(Xmlrdr);
return ds;
}
catch (Exception e)
{
throw e;
}
finally
{
//释放资源
if (Xmlrdr != null)
{
Xmlrdr.Close();
StrStream.Close();
StrStream.Dispose();
}
}
}
else
{
return null;
}
}
收获园豆:80
az235 | 园豆:8283 (大侠五级) | 2012-02-15 17:06

你好,我想把整个数据库的表 数据导出到一个xml文件,请问主要代码怎么写啊。在循环导出为xml时已经将每个表导出到单个xml了。

支持(0) 反对(0) flover7 | 园豆:84 (初学一级) | 2012-02-15 17:11
用这个例子
<%@ Page Language="C#" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
TextBox1.Text = "PROVIDER=SQLOLEDB; Data Source=.; Initial Catalog=pubs;User ID=sa;Password=;";
Label1.Text = "";
}
}
void CreateXml(object sender, EventArgs e) {
OleDbConnection dataConn = new OleDbConnection(TextBox1.Text);
Uri Path = Request.Url;
String ServerUrl = Path.ToString();
ServerUrl = ServerUrl.Substring(0,ServerUrl.LastIndexOf("/") +1 );
try {
Literal1.Text = "已经在你的相同目录下创建了一下文件:<br/>" ;
dataConn.Open();
DataTable schemaTable = dataConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
for(int I = 0; I < schemaTable.Rows.Count; I++){
OleDbDataAdapter dbAdapter= new OleDbDataAdapter("select * from [" + schemaTable.Rows[I].ItemArray[2].ToString() + "]",dataConn);
DataSet tableData = new DataSet();
dbAdapter.Fill(tableData,schemaTable.Rows[I].ItemArray[2].ToString());
tableData.WriteXml(HttpContext.Current.Server.MapPath(schemaTable.Rows[I].ItemArray[2].ToString() + ".xml"));
Literal1.Text = Literal1.Text + "<a href='" + ServerUrl + schemaTable.Rows[I].ItemArray[2].ToString() + ".xml'>" + schemaTable.Rows[I].ItemArray[2].ToString() + ".xml</a><br/>";
}
}
catch(Exception ex) {
Label1.Text = ex.Message.ToString();
}
finally {
dataConn.Close();
}
}
</script>
<html>
<head>
<title>leDb2XML:数据库转换成XML文件例子</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h2><font face="宋体">OleDb2XML:数据库转换成XML文件例子</font>
</h2>
</center>
<form runat="server">
<font face="宋体">
<p align="center">请输入OleDb连接字符串,然后点“生成XML文件”按钮。</p>
<p align="center">
<asp:TextBox id="TextBox1" runat="server" Width="600"></asp:TextBox>
</p>
<p align="center">
<asp:Button id="Button1" onclick="CreateXml" runat="server" Text="生成XML文件"></asp:Button>
</p>
<p align="center">
<asp:Label id="Label1" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
</p>
<p align="center">
<asp:Literal id="Literal1" runat="server"></asp:Literal>
</p>
</font>
</form>
</body>
</html>


@flover7: 

支持(0) 反对(0) az235 | 园豆:8283 (大侠五级) | 2012-02-15 17:14
@az235: 我的是winfrom 的,我已经将数据库中的各个表数据循环导出到相应的xml文件中,就是想在循环导出时合并xml,最后把整个数据库的数据导出到一个xml,就是在Merge时出问题了

#region 将数据导出对应的表xml
SqlDataAdapter daTable = new SqlDataAdapter("select name from sysobjects where xtype='U' order by name asc", conn);
DataTable dTable = new DataTable();
daTable.Fill(dTable);
DataSet dsTable = new DataSet();
dsTable.Tables.Add(dTable);

DataSet dsAll = new DataSet();
string ds = null;

for (int i = 0; i < dsTable.Tables[0].Rows.Count; i++)
{

string sqlTable = dsTable.Tables[0].Rows[i][0].ToString();
string fileName = "D:\\Project" + "\\" + "ExprotToXml" + "\\" + "XML" + "\\" + sqlTable + ".xml";
string sql = "select * from " + sqlTable + "";
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataTable dt1 = new DataTable(sqlTable);
sda.Fill(dt1);
DataSet ds1 = new DataSet();
ds1.Tables.Add(dt1);
ds1.WriteXml(fileName);

}

#endregion


@az235: 

支持(0) 反对(0) flover7 | 园豆:84 (初学一级) | 2012-02-16 10:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册