如何将oracle或者sqlserver表数据转换成xml数据

20 篇文章 2 订阅

oracle:

try
            {
                string tempTableName = "aaa";//数据库表名
                String tempRowName = "row";//节点
                String tempSavePath = "D:/Download/Master/users.xml";//保存地址
                DataTable datatd = new DataTable();
                using (OracleConnection conn = new OracleConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_DB"].ToString();
                    using (OracleCommand sqlcomm = conn.CreateCommand())
                    {
                        sqlcomm.CommandText = "select * from " + tempTableName;
                        OracleDataAdapter tempa = new OracleDataAdapter(sqlcomm);
                        tempa.Fill(datatd);
                    }
                }
                XElement tempDoc = new XElement(tempTableName);
                for (int i = 0; i < datatd.Rows.Count; i++)
                {
                    var tempElement = new XElement(tempRowName);
                    for (int j = 0; j < datatd.Columns.Count; j++)
                    {
                        tempElement.Add(new XElement(datatd.Columns[j].ColumnName, datatd.Rows[i][j] == DBNull.Value ? String.Empty : datatd.Rows[i][j]));
                    }
                    tempDoc.Add(tempElement);
                }
                tempDoc.Save(tempSavePath);

 

 

sqlserver:

string tempTableName = "ProductType";//数据库表名
String tempRowName = "row";//节点
String tempSavePath = "../Employees.xml";//保存地址
DataTable datatd = new DataTable();
using (SqlConnection sqlcon = new SqlConnection())
{
sqlcon.ConnectionString = "server=;database=;uid=sa;pwd=";
using (SqlCommand sqlcomm = sqlcon.CreateCommand())
{
sqlcomm.CommandText = "select * from " + tempTableName;
SqlDataAdapter tempa = new SqlDataAdapter(sqlcomm);
tempa.Fill(datatd);
}
}
XElement tempDoc = new XElement(tempTableName);
for (int i = 0; i < datatd.Rows.Count; i++)
{
var tempElement = new XElement(tempRowName);
for (int j = 0; j < datatd.Columns.Count; j++)
{
tempElement.Add(new XElement(datatd.Columns[j].ColumnName, datatd.Rows[i][j] == DBNull.Value ? String.Empty : datatd.Rows[i][j]));
}
tempDoc.Add(tempElement);
}
tempDoc.Save(tempSavePath);

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值