-- 作者:lhs0217
-- 发布时间:3/17/2009 4:43:00 PM
-- XML与SQL之间的相互转换(c#)?[求助]
有兴趣的进来讨论下,我只能做出单表的转换。多表的该用什么方法,具体该怎么做? 初学c#。。。 单表转换我做的代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data.SqlClient; using System.Data; using System.Xml; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DBtoXML { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“bookOrderDataSet.Books”中。您可以根据需要移动或移除它。 this.booksTableAdapter.Fill(this.bookOrderDataSet.Books); } private void btnRE_Click(object sender, EventArgs e) { //创建到数据库的连接,并读取数据 SqlConnection ConBook = new SqlConnection("Server=localhost;Database=BookOrder;UID=sa;PWD="); SqlDataAdapter dadBook = new SqlDataAdapter("Select *From Books", ConBook); DataSet dstBook = new DataSet(); dadBook.Fill(dstBook, "Books"); DataTable dtblBook = dstBook.Tables["Books"]; XmlDocument xdoc = new XmlDocument(); //创建并添加XML文档声明; //<?xml version="1.0" encoding="utf-8" ?> XmlDeclaration xdexl = xdoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); //xdoc.AppendChild(xdecl); //创建并添加文档的根元素books XmlElement xe_books = xdoc.CreateElement("books"); xdoc.AppendChild(xe_books); //读取数据库中每一行的信息并加入到XML文档中 for (int i = 0; i < dtblBook.Rows.Count; i++) { XmlElement xe_book = xdoc.CreateElement("Book"); //添加book的子元素bookID XmlElement xe_bookID = xdoc.CreateElement("bookID"); xe_bookID.InnerText = dtblBook.Rows[i]["BookID"].ToString(); xe_book.AppendChild(xe_bookID); //添加book的子元素bookISIN XmlElement xe_bookISBN = xdoc.CreateElement("bookISBN"); xe_bookISBN.InnerText = dtblBook.Rows[i]["BookISBN"].ToString(); xe_book.AppendChild(xe_bookISBN); //添加book的子元素bookName XmlElement xe_bookName = xdoc.CreateElement("bookName"); xe_bookName.InnerText = dtblBook.Rows[i]["BookName"].ToString(); xe_book.AppendChild(xe_bookName); //添加book的子元素bookCategory XmlElement xe_bookCategory = xdoc.CreateElement("bookCategory"); xe_bookCategory.InnerText = dtblBook.Rows[i]["BookCategory"].ToString(); xe_book.AppendChild(xe_bookCategory); //添加book的子元素bookAuthor XmlElement xe_bookAuthor = xdoc.CreateElement("bookAuthor"); xe_bookAuthor.InnerText = dtblBook.Rows[i]["BookAuthor"].ToString(); xe_book.AppendChild(xe_bookAuthor); //添加book的子元素bookPrice XmlElement xe_bookPrice = xdoc.CreateElement("bookPrice"); xe_bookPrice.InnerText = dtblBook.Rows[i]["BookPrice"].ToString(); xe_book.AppendChild(xe_bookPrice); //添加book的子元素bookPublisher XmlElement xe_bookPublisher = xdoc.CreateElement("bookPublisher"); xe_bookPublisher.InnerText = dtblBook.Rows[i]["BookPublisher"].ToString(); xe_book.AppendChild(xe_bookPublisher); //将book元素添加为文档元素books的子元素 xdoc.DocumentElement.AppendChild(xe_book); } SaveFileDialog SaveFile = new SaveFileDialog(); SaveFile.InitialDirectory = "D:\\"; SaveFile.Filter = "XML Files (*.xml)|*.xml"; SaveFile.FilterIndex = 1; // 如果文件名非空则保存 if (SaveFile.ShowDialog() == DialogResult.OK && SaveFile.FileName != "") { XmlDocument doc = new XmlDocument(); doc.LoadXml(xdoc.OuterXml); doc.Save(SaveFile.FileName); tbxXML.Text = xdoc.OuterXml; } //xdoc.Save(@"E:\DBtoXML\books.xml"); } } }
|