将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();
}
}
}
}
}