2013年6月4日 星期二

C# DataTable to XML

[C#]DataTable to XML


  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Web;
  8.  
  9. using System.Web.UI;
  10.  
  11. using System.Web.UI.WebControls;
  12.  
  13. using System.Data.SqlClient;
  14.  
  15. using System.Data;
  16.  
  17. using System.Xml;
  18.  
  19. using System.Xml.Linq;
  20.  
  21. using System.Text;
  22.  
  23.  
  24.  
  25. public partial class Test_tsDataTabletoXML : System.Web.UI.Page
  26.  
  27. {
  28.  
  29.     protected void Page_Load(object sender, EventArgs e)
  30.  
  31.     {
  32.  
  33.         try
  34.  
  35.         {
  36.  
  37.             SqlConnection cn = new SqlConnection();
  38.  
  39.             cn.ConnectionString = "Data Source=localhost;Initial Catalog=db1;Persist Security Info=True;User ID=sa;Password=123456";
  40.  
  41.  
  42.  
  43.             SqlCommand cmd = new SqlCommand();
  44.  
  45.             cmd.Connection = cn;
  46.  
  47.             cmd.CommandText = "select * from Product";
  48.  
  49.  
  50.  
  51.             cn.Open();
  52.  
  53.             SqlDataAdapter da = new SqlDataAdapter();
  54.  
  55.             da.SelectCommand = cmd;
  56.  
  57.             DataSet ds = new DataSet("ds1");
  58.  
  59.             da.Fill(ds, "Product");
  60.  
  61.             DataTable dt = ds.Tables["Product"];
  62.  
  63.  
  64.  
  65.             // XML Setting
  66.  
  67.             XmlWriterSettings settings = new XmlWriterSettings();
  68.  
  69.             settings.Indent = false;
  70.  
  71.             settings.Encoding = settings.Encoding = Encoding.GetEncoding("utf-8");
  72.  
  73.  
  74.  
  75.             // XML Write
  76.  
  77.             XElement tElement = new XElement("DataTable", "");
  78.  
  79.             using (XmlWriter xmlWriter = XmlWriter.Create("c:\\Product.xml", settings))
  80.  
  81.             {
  82.  
  83.                 // Write the data set to the writer.
  84.  
  85.                 tElement = DataTableToXml(dt, "root");
  86.  
  87.                 tElement.WriteTo(xmlWriter);
  88.  
  89.             }
  90.  
  91.         }
  92.  
  93.         catch (Exception ex)
  94.  
  95.         {
  96.  
  97.             throw ex;
  98.  
  99.         }
  100.  
  101.     }
  102.  
  103.  
  104.  
  105.     /// <summary>
  106.  
  107.     /// DataTable to XML
  108.  
  109.     /// </summary>
  110.  
  111.     /// <param name="pAttributeTable"></param>
  112.  
  113.     /// <param name="tParentTag"></param>
  114.  
  115.     /// <returns></returns>
  116.  
  117.     private XElement DataTableToXml(DataTable pAttributeTable, string tParentTag)
  118.  
  119.     {
  120.  
  121.         if (pAttributeTable == null)
  122.  
  123.             return null;
  124.  
  125.  
  126.  
  127.         XElement tParent = null;
  128.  
  129.         try
  130.  
  131.         {
  132.  
  133.             tParent = new XElement(tParentTag);  // 集合Tag的名字
  134.  
  135.             //每個DataRow為屬性分類
  136.  
  137.             foreach (DataRow tRow in pAttributeTable.Rows)
  138.  
  139.             {
  140.  
  141.                 XElement tChild = new XElement("DataRow");
  142.  
  143.                 //每個DataRow的DataColumn為屬性分類裡的項目
  144.  
  145.                 foreach (DataColumn tColumn in pAttributeTable.Columns)
  146.  
  147.                 {
  148.  
  149.                     tChild.Add(new XElement(tColumn.ColumnName, tRow[tColumn]));
  150.  
  151.                 }
  152.  
  153.                 tParent.Add(tChild);
  154.  
  155.             }
  156.  
  157.         }
  158.  
  159.         catch (Exception ex)
  160.  
  161.         {
  162.  
  163.             throw ex;
  164.  
  165.         }
  166.  
  167.         return tParent;
  168.  
  169.     }
  170.  
  171. }

沒有留言:

張貼留言