xml获取配置DataTable
在CSDN写博客时,经常遇到需要绑定数据源的时候,可是自己从数据库获取数据的函数都是已经封装好了的,这样别人复制你的代码,要想看到结果,还得修改修改,很是麻烦,博客实例,数据源简单点就好,这样别人写你写的实例时,不用新建数据库,或者不用自己整个获取数据源的函数,效率低,我要的是快速看运行结果,实例是否是正确,能够把你的代码复制下来,F5运行就可以直接看到效果。所以我就写了一个通过XML获取配置DataTable.这样以后自己写博客,需要绑定数据源的时候,不同的数据源,修改一下XML就可以了。
下面的实例的所有数据源都是通过xml获取的
一,效果图
核心代码:
public DataTable getDTXML(string xmlname) { DataTable result = new DataTable(); string fileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Xml\\" + xmlname + ".xml";//xml的物理路径 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNodeList xWitsTablesList = xmlDoc.SelectNodes("/config"); foreach (XmlNode xOracleNode in xWitsTablesList) { foreach (XmlNode node2 in xOracleNode.ChildNodes) { if (node2.Name == "Header") { // //绑定表头 foreach (XmlNode node3 in node2.Attributes) { result.Columns.Add(node3.Value); } } else { //数据行 int i = 0;//列标志 DataRow dr = result.NewRow(); foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } result.Rows.Add(dr); } } } return result; }
xml示例
<?xml version="1.0" encoding="utf-8" ?> <config> <Header col1="班级编号" col2="班级名称" col3="年级" col4="学校" col5="男生" col6="女生" col7="人数"/> <row col1="11" col2="一(一)班" col3="初一" col4="三峡高级中学" col5="20" col6="30" col7="50"/> <row col1="12" col2="一(二)班" col3="初一" col4="三峡高级中学" col5="18" col6="12" col7="30"/> <row col1="13" col2="一(三)班" col3="初一" col4="三峡高级中学" col5="16" col6="26" col7="42"/> <row col1="14" col2="一(四)班" col3="初一" col4="三峡高级中学" col5="23" col6="17" col7="40"/> </config>
二,下面贴出源码:
1)表头配置.xml文件:
<?xml version="1.0" encoding="utf-8" ?> <config> <Header col1="班级编号" col2="班级名称" col3="年级" col4="学校" col5="男生" col6="女生" col7="人数"/> <row col1="11" col2="一(一)班" col3="初一" col4="三峡高级中学" col5="20" col6="30" col7="50"/> <row col1="12" col2="一(二)班" col3="初一" col4="三峡高级中学" col5="18" col6="12" col7="30"/> <row col1="13" col2="一(三)班" col3="初一" col4="三峡高级中学" col5="16" col6="26" col7="42"/> <row col1="14" col2="一(四)班" col3="初一" col4="三峡高级中学" col5="23" col6="17" col7="40"/> </config>
2)readXml.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readXml.aspx.cs" Inherits="readXml" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link rel="Stylesheet" type="text/css" href="Css/readXml.css" /> </head> <body> <form id="form1" runat="server"> <div style=" margin-left:30px;"><h2 style="color:Yellow; background-color:Gray;" >通过解析XML获取table</h2></div> <div class="gv_one"> <asp:GridView runat="server" ID="gv_xml" CellPadding="4" Width="500px" ForeColor="#333333" GridLines="None" > <AlternatingRowStyle BackColor="White" /> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <SortedAscendingCellStyle BackColor="#FDF5AC" /> <SortedAscendingHeaderStyle BackColor="#4D0000" /> <SortedDescendingCellStyle BackColor="#FCF6C0" /> <SortedDescendingHeaderStyle BackColor="#820000" /> </asp:GridView> </div> <div class="gv_Two"> <asp:GridView runat="server" ID="gv_xmlOne" CellPadding="4" Width="500px" ForeColor="#333333" GridLines="None" > <AlternatingRowStyle BackColor="White" /> <EditRowStyle BackColor="#2461BF" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#EFF3FB" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F5F7FB" /> <SortedAscendingHeaderStyle BackColor="#6D95E1" /> <SortedDescendingCellStyle BackColor="#E9EBEF" /> <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView> </div> </form> </body> </html>
3)readXml.aspx.cs
using System; using System.Collections; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.Data; using System.Data.SqlClient; using System.Xml; using System.Data.OleDb; using IsXMLUtility; public partial class readXml : System.Web.UI.Page { XmlHelper xmlHelper = new XmlHelper(); protected void Page_Load(object sender, EventArgs e) { InitGV(); } private void InitGV() { DataTable dt1 = xmlHelper.getDTXML("表头配置"); gv_xml.DataSource = dt1.DefaultView; gv_xml.DataBind(); dt1 = xmlHelper.getDTXML("表头配置", "N0", 1); gv_xmlOne.DataSource = dt1.DefaultView; gv_xmlOne.DataBind(); } }
4) XmlHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Xml; namespace IsXMLUtility { /// <summary> ///XmlHelper 的摘要说明 /// </summary> public class XmlHelper { public XmlHelper() { // //TODO: 在此处添加构造函数逻辑 // } public DataTable getDTXML(string xmlname) { DataTable result = new DataTable(); string fileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Xml\\" + xmlname + ".xml";//xml的物理路径 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNodeList xWitsTablesList = xmlDoc.SelectNodes("/config"); foreach (XmlNode xOracleNode in xWitsTablesList) { foreach (XmlNode node2 in xOracleNode.ChildNodes) { if (node2.Name == "Header") { // //绑定表头 foreach (XmlNode node3 in node2.Attributes) { result.Columns.Add(node3.Value); } } else { //数据行 int i = 0;//列标志 DataRow dr = result.NewRow(); foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } result.Rows.Add(dr); } } } return result; } /// <summary> /// /// </summary> /// <param name="xmlname">xml名字</param> /// <param name="Header">序号列名字</param> /// <param name="colsNumber">序号开始编号</param> /// <returns></returns> public DataTable getDTXML(string xmlname, string Header, int colsNumber) { DataTable result = new DataTable(); if (Header == "") Header = "N0"; if (colsNumber < 0) colsNumber = 1; result.Columns.Add(Header); string fileName = HttpContext.Current.Request.PhysicalApplicationPath + "\\Xml\\" + xmlname + ".xml";//xml的物理路径 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNodeList xWitsTablesList = xmlDoc.SelectNodes("/config"); foreach (XmlNode xOracleNode in xWitsTablesList) { foreach (XmlNode node2 in xOracleNode.ChildNodes) { if (node2.Name == "Header") { //绑定表头 foreach (XmlNode node3 in node2.Attributes) { result.Columns.Add(node3.Value); } } else { //数据行 int i = 1;//列标志 DataRow dr = result.NewRow(); dr[0] = colsNumber; foreach (XmlNode node4 in node2.Attributes) { dr[i] = node4.Value.ToString(); i++; } colsNumber++; result.Rows.Add(dr); } } } return result; } } }
5) readXml.css
body{ margin:0 auto; padding:0px; font-family:'宋体'; } .gv_one { margin-left:30px; width:400px; height:200px; margin-top:20px; border:#A8B7CC solid 1px; background-color:#FFFFFF; overflow: scroll; SCROLLBAR-FACE-COLOR:#E0EDFE; PADDING-BOTTOM: 0px; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; SCROLLBAR-3DLIGHT-COLOR:#ffffff; SCROLLBAR-ARROW-COLOR:#95AFD4; PADDING-TOP: 0px; SCROLLBAR-TRACK-COLOR: #ffffff; SCROLLBAR-DARKSHADOW-COLOR: #ffffff; LETTER-SPACING: 1pt; } .gv_Two { margin-left:30px; width:400px; height:200px; margin-top:20px; border:#A8B7CC solid 1px; background-color:#FFFFFF; overflow: scroll; SCROLLBAR-FACE-COLOR:#E0EDFE; PADDING-BOTTOM: 0px; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #cccccc; SCROLLBAR-3DLIGHT-COLOR:#ffffff; SCROLLBAR-ARROW-COLOR:#95AFD4; PADDING-TOP: 0px; SCROLLBAR-TRACK-COLOR: #ffffff; SCROLLBAR-DARKSHADOW-COLOR: #ffffff; LETTER-SPACING: 1pt; }