将某一目录下的所有相同格式的 XML文件绑定到不同的DataGrid

来源:互联网 发布:旺宝软件 编辑:程序博客网 时间:2024/05/18 06:34
   将某一目录下的所有相同格式的 XML文件绑定到不同的DataGrid的方法。
  
  <%@ Page Language="<a href="http://dev.21tx.com/language/vb/" target="_blank">VB</a>"%>
  <%@ Import NameSpace = "System" %>
  <%@ Import NameSpace = "System.<a href="http://dev.21tx.com/web/xml/" target="_blank">XML</a>" %>
  <%@ Import NameSpace = "System.IO" %>
  <%@ Import NameSpace = "System.Collections" %>
  <%@ Import NameSpace = "System.<a href="http://dev.21tx.com/web/" target="_blank">Web</a>" %>
  <%@ Import NameSpace = "System.Web.UI" %>
  <%@ Import NameSpace = "System.Web.UI.<a href="http://dev.21tx.com/dotnet/aspnet/webcontrols/" target="_blank">WebControls</a>" %>
  <%@ Import NameSpace = "System.Data" %>
  
  <script runat=Server>
  Sub Page_Load( sender as object, e as System.EventArgs)
   Dim dir As DirectoryInfo = New DirectoryInfo("D:/Web")
   Dim files As FileInfo() = dir.GetFiles()
   Dim count As Integer = files.Length
   Dim i As Integer
   For i = 0 To count - 1
   If files(i).Name.SubString(files(i).Name.LastIndexOf(".")) = ".xml" Then
   Dim ds As New DataSet()
   'ds.ReadXml("d:/Web/c.xml")
   ds.ReadXml(files(i).FullName)
   Dim dt as DataGrid = New DataGrid()
   dt.ID = "DataGrid" + i.ToString()
   dt.AutoGenerateColumns=false
  
   Dim MyName As BoundColumn = New BoundColumn()
   Dim MyProductID As BoundColumn = New BoundColumn()
   Dim Price As BoundColumn = New BoundColumn()
   Dim Quantity As BoundColumn = New BoundColumn()
  
   MyName.HeaderText="名字"
   MyName.DataField="Name"
  
   MyProductID.HeaderText="序号"
   MyProductID.DataField="ProductID"
  
   Price.HeaderText="价格"
   Price.DataField="Price"
  
   Quantity.HeaderText="数量"
   Quantity.DataField="Quantity"
  
   dt.Columns.AddAt(0, MyName)
   dt.Columns.AddAt(1, MyProductID)
   dt.Columns.AddAt(2, Price)
   dt.Columns.AddAt(3, Quantity)
  
   dt.DataSource = ds.Tables("Product")
   dt.DataBind()
   Me.Controls.Add(dt)
   End If
   Next
  End Sub
  </script>
  <form runat=server>
  </form>
  
  C#写法
  
  <%@ Page Language="C#"%>
  <%@ Import NameSpace = "System" %>
  <%@ Import NameSpace = "System.Xml" %>
  <%@ Import NameSpace = "System.IO" %>
  <%@ Import NameSpace = "System.Collections" %>
  <%@ Import NameSpace = "System.Web" %>
  <%@ Import NameSpace = "System.Web.UI" %>
  <%@ Import NameSpace = "System.Web.UI.WebControls" %>
  <%@ Import NameSpace = "System.Data" %>
  
  <script runat=Server>
  void Page_Load(object sender, System.EventArgs e)
  {
   DirectoryInfo dir = new DirectoryInfo("D://Web");
   FileInfo[] files = dir.GetFiles();
   int count = files.Length;
   for(int i = 0;i<count;i++)
   {
   if(files[i].Name.Substring(files[i].Name.LastIndexOf(".")) == ".xml")
   {
   DataSet ds = new DataSet();
   //'ds.ReadXml("d://Web//c.xml");
   ds.ReadXml(files[i].FullName);
   DataGrid dt = new DataGrid();
   dt.ID = "DataGrid" + i.ToString();
   dt.AutoGenerateColumns=false;
  
   BoundColumn MyName = new BoundColumn();
   BoundColumn MyProductID = new BoundColumn();
   BoundColumn Price= new BoundColumn();
   BoundColumn Quantity = new BoundColumn();
  
   MyName.HeaderText="名字";
   MyName.DataField="Name";
  
   MyProductID.HeaderText="序号";
   MyProductID.DataField="ProductID";
  
   Price.HeaderText="价格";
   Price.DataField="Price";
  
   Quantity.HeaderText="数量";
   Quantity.DataField="Quantity";
  
   dt.Columns.AddAt(0, MyName);
   dt.Columns.AddAt(1, MyProductID);
   dt.Columns.AddAt(2, Price);
   dt.Columns.AddAt(3, Quantity);
  
   dt.DataSource = ds.Tables["Product"];
   dt.DataBind();
   this.Controls.Add(dt);
   }
   }
  }
  </script>
  <form runat=server>
  </form>
  
  xml文件格式:
  
  <?xml version="1.0" encoding="gb2312"?>
  <DataSet>
   <Product>
   <Name>[孟宪会之精彩世界]</Name>
   <ProductID>1</ProductID>
   <Price>12000</Price>
   <Quantity>1</Quantity>
   </Product>
   <Product>
   <Name>http://dotnet.<a href="http://dev.21tx.com/web/asp/" target="_blank">ASP</a>x.cc</Name>
   <ProductID>2</ProductID>
   <Price>12000</Price>
   <Quantity>2</Quantity>
   </Product>
   <Product>
   <Name>http://xml.sz.luohuedu<a href="http://dev.21tx.com/dotnet/" target="_blank">.net</a>/xml/</Name>
   <ProductID>3</ProductID>
   <Price>18000</Price>
   <Quantity>2</Quantity>
   </Product>
  </DataSet>
原创粉丝点击