XML数据导入进数据库,和从数据导出XML数据

将XML数据导入数据库:导入到两张表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class SaveSQL : System.Web.UI.Page
    {
        string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Save_Click(object sender, EventArgs e)
        {
            string title;
            string author;
            string year;
            string price;
            string gender;
            string age;
            Stream stream = File.OpenRead(Server.MapPath("book.xml"));
            XDocument document = XDocument.Load(stream);
            stream.Dispose();
            foreach (XElement item in document.Root.Descendants("book"))
            {
                title = item.Element("title").Value;
                author = item.Element("author").Value;
                year = item.Element("year").Value;
                price = item.Element("price").Value;
                using (SqlConnection con = new SqlConnection(strcon))
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "insert into T_XmlBook(title,author,year,price)values(@title,@author,@year,@price)";
                        con.Open();
                        cmd.Parameters.Add("@title", title);
                        cmd.Parameters.Add("@author", author);
                        cmd.Parameters.Add("@year", year);
                        cmd.Parameters.Add("@price", price);
                        cmd.ExecuteNonQuery();
                    }
                }
                foreach (XElement items in document.Root.Descendants("book"))
                {
                    gender = items.Element("author").Attribute("gender").Value;
                    age = items.Element("author").Attribute("age").Value;
                    using (SqlConnection con = new SqlConnection(strcon))
                    {
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandText = "select id from T_XmlBook where title='" + title + "'";
                            con.Open();
                            int id=Convert.ToInt32(cmd.ExecuteScalar());
                            cmd.CommandText = "insert into T_XmlBooks (titleid,gender,age)values(@titleid,@gender,@age)";
                          
                            cmd.Parameters.AddWithValue("@titleid", id);
                            cmd.Parameters.AddWithValue("@gender", gender);
                            cmd.Parameters.AddWithValue("@age", age);
                            cmd.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
    }
}

 

 

从数据中导出数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class educe : System.Web.UI.Page
    {
        string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void edu_Click(object sender, EventArgs e)
        {
            using (SqlConnection con=new SqlConnection(strcon))
            {
                using (SqlCommand cmd=con.CreateCommand())
                {
                    cmd.CommandText = "select * from T_XmlBook";
                    con.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    XElement xmlbookstore = new XElement("bookstore");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        XElement xmlbook = new XElement("book");
                        XElement xmltitle = new XElement("title", dt.Rows[0][1]);
                        XElement xmlauthor = new XElement("author", dt.Rows[0][2]);
                        XElement xmlyear = new XElement("year", dt.Rows[0][3]);
                        XElement xmlprice = new XElement("price", dt.Rows[0][4]);
                        xmlbook.Add(xmltitle);
                        xmlbook.Add(xmlauthor);
                        xmlbook.Add(xmlyear);
                        xmlbook.Add(xmlprice);
                        xmlbookstore.Add(xmlbook);
                    }
                    FileStream stream = File.OpenWrite(@"D:\book.xml");
                    StreamWriter writer = new StreamWriter(stream);
                    writer.WriteLine(xmlbookstore.ToString());
                    writer.Flush();

                    stream.Dispose();
                }
            }
           

        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值