xml获取配置DataTable

在CSDN写博客时,经常遇到需要绑定数据源的时候,可是自己从数据库获取数据的函数都是已经封装好了的,这样别人复制你的代码,要想看到结果,还得修改修改,很是麻烦,博客实例,数据源简单点就好,这样别人写你写的实例时,不用新建数据库,或者不用自己整个获取数据源的函数,效率低,我要的是快速看运行结果,实例是否是正确,能够把你的代码复制下来,F5运行就可以直接看到效果。所以我就写了一个通过XML获取配置DataTable.这样以后自己写博客,需要绑定数据源的时候,不同的数据源,修改一下XML就可以了。

下面的实例的所有数据源都是通过xml获取的

一,效果图

核心代码:

 

[csharp][/csharp] view plaincopy

  1. public DataTable getDTXML(string xmlname)
  2.         {
  3.             DataTable result = new DataTable();
  4.             string fileName = HttpContext.Current.Request.PhysicalApplicationPath + “\\Xml\\” + xmlname + “.xml”;//xml的物理路径
  5.             XmlDocument xmlDoc = new XmlDocument();
  6.             xmlDoc.Load(fileName);
  7.             XmlNodeList xWitsTablesList = xmlDoc.SelectNodes(“/config”);
  8.             foreach (XmlNode xOracleNode in xWitsTablesList)
  9.             {
  10.                 foreach (XmlNode node2 in xOracleNode.ChildNodes)
  11.                 {
  12.                     if (node2.Name == “Header”)
  13.                     {
  14.                         //   //绑定表头
  15.                         foreach (XmlNode node3 in node2.Attributes)
  16.                         {
  17.                             result.Columns.Add(node3.Value);
  18.                         }
  19.                     }
  20.                     else
  21.                     {
  22.                         //数据行
  23.                         int i = 0;//列标志
  24.                         DataRow dr = result.NewRow();
  25.                         foreach (XmlNode node4 in node2.Attributes)
  26.                         {
  27.                             dr[i] = node4.Value.ToString();
  28.                             i++;
  29.                         }
  30.                         result.Rows.Add(dr);
  31.                     }
  32.                 }
  33.             }
  34.             return result;
  35.         }

xml示例

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″ ?>
  2. <config>
  3.     <Header  col1=”班级编号” col2=”班级名称” col3=”年级” col4=”学校” col5=”男生” col6=”女生”  col7=”人数”/>
  4.     <row     col1=”11″ col2=”一(一)班” col3=”初一” col4=”三峡高级中学” col5=”20″ col6=”30″  col7=”50″/>
  5.     <row     col1=”12″ col2=”一(二)班” col3=”初一” col4=”三峡高级中学” col5=”18″ col6=”12″  col7=”30″/>
  6.     <row     col1=”13″ col2=”一(三)班” col3=”初一” col4=”三峡高级中学” col5=”16″ col6=”26″  col7=”42″/>
  7.     <row     col1=”14″ col2=”一(四)班” col3=”初一” col4=”三峡高级中学” col5=”23″ col6=”17″  col7=”40″/>
  8. </config>

 

 

 

二,下面贴出源码:

  1)表头配置.xml文件:

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″ ?>
  2. <config>
  3.     <Header  col1=”班级编号” col2=”班级名称” col3=”年级” col4=”学校” col5=”男生” col6=”女生”  col7=”人数”/>
  4.     <row     col1=”11″ col2=”一(一)班” col3=”初一” col4=”三峡高级中学” col5=”20″ col6=”30″  col7=”50″/>
  5.     <row     col1=”12″ col2=”一(二)班” col3=”初一” col4=”三峡高级中学” col5=”18″ col6=”12″  col7=”30″/>
  6.     <row     col1=”13″ col2=”一(三)班” col3=”初一” col4=”三峡高级中学” col5=”16″ col6=”26″  col7=”42″/>
  7.     <row     col1=”14″ col2=”一(四)班” col3=”初一” col4=”三峡高级中学” col5=”23″ col6=”17″  col7=”40″/>
  8. </config>

 

 

 2)readXml.aspx

 

[html][/html] view plaincopy

  1. <%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”readXml.aspx.cs” Inherits=”readXml” %>
  2. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
  3. <html xmlns=”http://www.w3.org/1999/xhtml”>
  4. <head runat=”server”>
  5.     <title></title>
  6.     <link rel=”Stylesheet” type=”text/css” href=”Css/readXml.css” />
  7. </head>
  8. <body>
  9.     <form id=”form1″ runat=”server”>
  10.     <div style=” margin-left:30px;”><h2 style=”color:Yellow; background-color:Gray;” >通过解析XML获取table</h2></div>
  11.     <div class=”gv_one”>
  12.        <asp:GridView runat=”server” ID=”gv_xml”
  13.             CellPadding=”4″ Width=”500px”  ForeColor=”#333333″ GridLines=”None” >
  14.            <AlternatingRowStyle BackColor=”White” />
  15.            <FooterStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”White” />
  16.            <HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”White” />
  17.            <PagerStyle BackColor=”#FFCC66″ ForeColor=”#333333″ HorizontalAlign=”Center” />
  18.            <RowStyle BackColor=”#FFFBD6″ ForeColor=”#333333″ />
  19.            <SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”Navy” />
  20.            <SortedAscendingCellStyle BackColor=”#FDF5AC” />
  21.            <SortedAscendingHeaderStyle BackColor=”#4D0000″ />
  22.            <SortedDescendingCellStyle BackColor=”#FCF6C0″ />
  23.            <SortedDescendingHeaderStyle BackColor=”#820000″ />
  24.        </asp:GridView>
  25.     </div>
  26.     <div class=”gv_Two”>
  27.        <asp:GridView runat=”server” ID=”gv_xmlOne”
  28.             CellPadding=”4″ Width=”500px” ForeColor=”#333333″ GridLines=”None” >
  29.            <AlternatingRowStyle BackColor=”White” />
  30.            <EditRowStyle BackColor=”#2461BF” />
  31.            <FooterStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />
  32.            <HeaderStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />
  33.            <PagerStyle BackColor=”#2461BF” ForeColor=”White” HorizontalAlign=”Center” />
  34.            <RowStyle BackColor=”#EFF3FB” />
  35.            <SelectedRowStyle BackColor=”#D1DDF1″ Font-Bold=”True” ForeColor=”#333333″ />
  36.            <SortedAscendingCellStyle BackColor=”#F5F7FB” />
  37.            <SortedAscendingHeaderStyle BackColor=”#6D95E1″ />
  38.            <SortedDescendingCellStyle BackColor=”#E9EBEF” />
  39.            <SortedDescendingHeaderStyle BackColor=”#4870BE” />
  40.        </asp:GridView>
  41.     </div>
  42.     </form>
  43. </body>
  44. </html>

 

 

 3)readXml.aspx.cs

 

[csharp][/csharp] view plaincopy

  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Web.Services.Protocols;
  7. using System.Xml.Linq;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Xml;
  11. using System.Data.OleDb;
  12. using IsXMLUtility;
  13. public partial class readXml : System.Web.UI.Page
  14. {
  15.     XmlHelper xmlHelper = new XmlHelper();
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         InitGV();
  19.     }
  20.     private void InitGV()
  21.     {
  22.         DataTable dt1 = xmlHelper.getDTXML(“表头配置”);
  23.         gv_xml.DataSource = dt1.DefaultView;
  24.         gv_xml.DataBind();
  25.         dt1 = xmlHelper.getDTXML(“表头配置”, “N0”, 1);
  26.         gv_xmlOne.DataSource = dt1.DefaultView;
  27.         gv_xmlOne.DataBind();
  28.     }
  29. }

 

 

 4) XmlHelper.cs

 

[csharp][/csharp] view plaincopy

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Xml;
  7. namespace IsXMLUtility
  8. {
  9.     /// <summary>
  10.     ///XmlHelper 的摘要说明
  11.     /// </summary>
  12.     public class XmlHelper
  13.     {
  14.         public XmlHelper()
  15.         {
  16.             //
  17.             //TODO: 在此处添加构造函数逻辑
  18.             //
  19.         }
  20.         public DataTable getDTXML(string xmlname)
  21.         {
  22.             DataTable result = new DataTable();
  23.             string fileName = HttpContext.Current.Request.PhysicalApplicationPath + “\\Xml\\” + xmlname + “.xml”;//xml的物理路径
  24.             XmlDocument xmlDoc = new XmlDocument();
  25.             xmlDoc.Load(fileName);
  26.             XmlNodeList xWitsTablesList = xmlDoc.SelectNodes(“/config”);
  27.             foreach (XmlNode xOracleNode in xWitsTablesList)
  28.             {
  29.                 foreach (XmlNode node2 in xOracleNode.ChildNodes)
  30.                 {
  31.                     if (node2.Name == “Header”)
  32.                     {
  33.                         //   //绑定表头
  34.                         foreach (XmlNode node3 in node2.Attributes)
  35.                         {
  36.                             result.Columns.Add(node3.Value);
  37.                         }
  38.                     }
  39.                     else
  40.                     {
  41.                         //数据行
  42.                         int i = 0;//列标志
  43.                         DataRow dr = result.NewRow();
  44.                         foreach (XmlNode node4 in node2.Attributes)
  45.                         {
  46.                             dr[i] = node4.Value.ToString();
  47.                             i++;
  48.                         }
  49.                         result.Rows.Add(dr);
  50.                     }
  51.                 }
  52.             }
  53.             return result;
  54.         }
  55.         /// <summary>
  56.         ///
  57.         /// </summary>
  58.         /// <param name=”xmlname”>xml名字</param>
  59.         /// <param name=”Header”>序号列名字</param>
  60.         /// <param name=”colsNumber”>序号开始编号</param>
  61.         /// <returns></returns>
  62.         public DataTable getDTXML(string xmlname, string Header, int colsNumber)
  63.         {
  64.             DataTable result = new DataTable();
  65.             if (Header == “”)
  66.                 Header = “N0”;
  67.             if (colsNumber < 0)
  68.                 colsNumber = 1;
  69.             result.Columns.Add(Header);
  70.             string fileName = HttpContext.Current.Request.PhysicalApplicationPath + “\\Xml\\” + xmlname + “.xml”;//xml的物理路径
  71.             XmlDocument xmlDoc = new XmlDocument();
  72.             xmlDoc.Load(fileName);
  73.             XmlNodeList xWitsTablesList = xmlDoc.SelectNodes(“/config”);
  74.             foreach (XmlNode xOracleNode in xWitsTablesList)
  75.             {
  76.                 foreach (XmlNode node2 in xOracleNode.ChildNodes)
  77.                 {
  78.                     if (node2.Name == “Header”)
  79.                     {
  80.                         //绑定表头
  81.                         foreach (XmlNode node3 in node2.Attributes)
  82.                         {
  83.                             result.Columns.Add(node3.Value);
  84.                         }
  85.                     }
  86.                     else
  87.                     {
  88.                         //数据行
  89.                         int i = 1;//列标志
  90.                         DataRow dr = result.NewRow();
  91.                         dr[0] = colsNumber;
  92.                         foreach (XmlNode node4 in node2.Attributes)
  93.                         {
  94.                             dr[i] = node4.Value.ToString();
  95.                             i++;
  96.                         }
  97.                         colsNumber++;
  98.                         result.Rows.Add(dr);
  99.                     }
  100.                 }
  101.             }
  102.             return result;
  103.         }
  104.     }
  105. }

5) readXml.css

 

 

[css][/css] view plaincopy

  1. body{
  2. margin:0 auto;
  3. padding:0px;
  4. font-family:’宋体’;
  5. }
  6. .gv_one
  7. {
  8.     margin-left:30px;
  9.     width:400px;
  10.     height:200px;
  11.     margin-top:20px;
  12.     border:#A8B7CC solid 1px;
  13.     background-color:#FFFFFF;
  14.     overflow: scroll;
  15.     SCROLLBAR-FACE-COLOR:#E0EDFE;
  16.     PADDING-BOTTOM: 0px;
  17.     SCROLLBAR-HIGHLIGHT-COLOR: #ffffff;
  18.     SCROLLBAR-SHADOW-COLOR: #cccccc;
  19.     SCROLLBAR-3DLIGHT-COLOR:#ffffff;
  20.     SCROLLBAR-ARROW-COLOR:#95AFD4;
  21.     PADDING-TOP: 0px;
  22.     SCROLLBAR-TRACK-COLOR: #ffffff;
  23.     SCROLLBAR-DARKSHADOW-COLOR: #ffffff;
  24.     LETTER-SPACING: 1pt;
  25. }
  26. .gv_Two
  27. {
  28.     margin-left:30px;
  29.     width:400px;
  30.     height:200px;
  31.     margin-top:20px;
  32.     border:#A8B7CC solid 1px;
  33.     background-color:#FFFFFF;
  34.     overflow: scroll;
  35.     SCROLLBAR-FACE-COLOR:#E0EDFE;
  36.     PADDING-BOTTOM: 0px;
  37.     SCROLLBAR-HIGHLIGHT-COLOR: #ffffff;
  38.     SCROLLBAR-SHADOW-COLOR: #cccccc;
  39.     SCROLLBAR-3DLIGHT-COLOR:#ffffff;
  40.     SCROLLBAR-ARROW-COLOR:#95AFD4;
  41.     PADDING-TOP: 0px;
  42.     SCROLLBAR-TRACK-COLOR: #ffffff;
  43.     SCROLLBAR-DARKSHADOW-COLOR: #ffffff;
  44.     LETTER-SPACING: 1pt;
  45. }

标签