Cách bind dữ liệu XML pptx

8 371 0
Cách bind dữ liệu XML pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Biên tập: thienthanit@yahoo.com Nguồn: Internet Trong phần này, ta sẽ tìm hiểu về cách bind dữ liệu XML vào một control hiển thị dữ liệu như GridView. Để thực hiện việc này, đầu tiên ta sẽ sử dụng control XmlDataSource quản lý dữ liệu từ file XML 1. Binding ở dạng Nonhierarchical Đây là dạng hiển thị dữ liệu đơn giản nhất, không có cấu trúc phân tầng. Tạo nguồn dữ liệu đại diện cho file XML <asp:xmldatasource ID="xmlSourceDVD" runat="server" DataFile="DVDList.xml"></asp:xmldatasource> Sau đó ta cần một GridView để hiển thị dữ liệu <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataSourceID="xmlSourceDVD" BackColor="#C0C0FF"> <AlternatingRowStyle BackColor="#E0E0E0" /> </asp:GridView> Trường hợp ta chỉ muốn hiển thị một số column, hãy thiết lập AutoGenerateColumns="false" <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" DataSourceID="xmlSourceDVD" BackColor="#C0C0FF"> <Columns> <asp:BoundField DataField="ID" HeaderText ="ID" /> <asp:BoundField DataField="Catagory" HeaderText="Catagory" SortExpression="Catagory" /> </Columns> <AlternatingRowStyle BackColor="#E0E0E0" /> </asp:GridView> Biên tập: thienthanit@yahoo.com Nguồn: Internet 2. Binding sử dụng XPath Trong các ví dụ trên, ta đã biết cách bind dữ liệu vào GridView. Tuy nhiên, với cách làm trên ta chỉ hiển thị các dữ tại cấp cha <DVD>, nếu muốn hiển thị các dữ liệu ở các node con thì ta phải dùng XPath. Tạo nguồn dữ liệu đại diện cho file XML <asp:xmldatasource ID=" xmlSourceDVD " runat="server" DataFile="DVDList.xml"></asp:xmldatasource> Sau đó ta cần một GridView để hiển thị dữ liệu <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="false" DataSourceID="xmlSourceDVD" BackColor="#C0C0FF"> <Columns> <asp:TemplateField HeaderText="DVD"> <ItemTemplate> <b><%# XPath("./Title") %> </b><br /> <%# XPath("./Description") %> </ItemTemplate> </asp:TemplateField> </Columns> <AlternatingRowStyle BackColor="#E0E0E0" /> </asp:GridView> Biên tập: thienthanit@yahoo.com Nguồn: Internet Trong trường hợp ta muốn hiển thị tên của các diễn viên, nếu chỉ áp dụng như trên thì cũng không thể thực hiện được vì <star> là node cháu của <DVD>. Để thực hiện việc này, thì trong XmlDataSource ta phải chỉ ra XPath. Tạo nguồn dữ liệu đại diện cho file XML <asp:xmldatasource ID="Xmldatasource1" runat="server" XPath="/DVDList/DVD/Stars/Star" DataFile="DVDList.xml"> </asp:xmldatasource> Tạo GridView để hiển thị dữ liệu <asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="false" DataSourceID="Xmldatasource1" BackColor="#C0C0FF" DataFile="DVDList.xml"> <Columns> <asp:TemplateField HeaderText="Star"> <ItemTemplate> <b><%# XPath(".") %> </b><br /> </ItemTemplate> </asp:TemplateField> </Columns> <AlternatingRowStyle BackColor="#E0E0E0" /> </asp:GridView> Biên tập: thienthanit@yahoo.com Nguồn: Internet 3. Binding sử dụng Nested Grids Một trong những cách khác để ta có thể hiển thị các node ở cấp dưới đó là sử dụng Nested Grids hay các các grid lồng nhau. Với các control hiện tại của VS2005 ta có thể thực hiện dễ dàng việc này mà không cần viết code. Ví dụ sau đây, ta sẽ hiển thị danh sách các phim và diễn viên tham gia. Tên của phim nằm trong thẻ Title, tên của diễn viên nằm trong thẻ <Star>. Hai thẻ này không đồng cấp, để hiển thị chúng, ta sẽ dung 2 GridView lồng nhau. Tạo ra XmlDataSource <asp:XmlDataSource ID="xmlSourceDVD" runat="server" DataFile="DVDList.xml"> </asp:XmlDataSource> Tạo 2 GridView lồng nhau <asp:GridView ID="GridView1" runat="server" DataSourceID="xmlSourceDVD" AutoGenerateColumns="false" BackColor="#E0E0E0" CellPadding="0" Width="200px" > <Columns> <asp:TemplateField HeaderText="DVD"> <ItemTemplate> <b><%# XPath("./Title") %></b><br /> <%# XPath("./Description")%><br /> <br /><i>Stars </i> Tại thời điểm này, ta sẽ hiển thị được Tên và Giới thiệu của phim, ta cần có them 1 GridView để hiển thị danh sách diễn viên <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" BorderWidth="0" ShowHeader="False" DataSource='<%# XPathSelect("./Stars/Star") %>'> Biên tập: thienthanit@yahoo.com Nguồn: Internet DataSoruce của GridView này ta sử dụng hàm XPathSelect(), tham số truyền vào là 1 XPath và giá trị trị về là một XmlNodeList. Cuối cùng ta tạo template cho GridView2 và hiển thị dữ liệu <Columns> <asp:TemplateField> <ItemTemplate> &nbsp<%# XPath(".") %><br /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </ItemTemplate> </asp:TemplateField> </Columns> <AlternatingRowStyle BackColor="#C0FFFF" ForeColor="Black" /> </asp:GridView> Biên tập: thienthanit@yahoo.com Nguồn: Internet 4. XML và Dataset Ta biết rằng XML có khả năng lưu trữ liệu có cấu trúc giống như các table trong CSDL. Trong rất nhiều trường hợp ta sẽ cần chuyển toàn bộ dữ liệu trong bảng vào file XML và ngược lại. ADO.Net hỗ trợ chúng ta rất nhiều trong các thao tác liên quan tới XML và Dataset. WriteXml() và ReadXml() là 2 phương thức cơ bản nhất  WriteXml(): Ghi dữ liệu từ Dataset vào xml file, TextWriter hay XmlWriter.  SqlDataAdapter DA =  new SqlDataAdapter("SELECT * FROM Project", connectionString);  DataSet DS = new DataSet("DMS");  DA.Fill(DS, "Project"); DS.WriteXml(xmlFile,XmlWriteMode.WriteSchema);  ReadXml(): Tải dữ liệu từ xml file, TextReader hay XmlReader vào Dataset  DataSet DS = new DataSet("DMS"); DS.ReadXml(xmlFileName); Biên tập: thienthanit@yahoo.com Nguồn: Internet Truy cập Dataset thông qua XML Đây là cách thức giúp ta xử lý dữ liệu trong Dataset giống như với dữ liệu XML. Tức là thay vì xử dụng các hàm của Dataset thì ta sẽ sử dụng các hàm trong XML. Để thực hiện được việc này, ta cần tạo XmlDataDocument. XmlDataDocument xmlDataDoc = new XmlDataDocument(myDataSet); XmlDataDocument được kế thừa từ XmlDocument, nên nó sẽ bao gồm tất cả các phương thức và thuộc tính của XmlDocument để ta xử lý dữ liệu. Một điều quan trọng khác là: sau lệnh khởi tạo trên, ta sẽ có sự đồng bộ giữa Dataset và XML, bất cứ sự thay đổi nào từ 1 phía sẽ đồng thời cập nhật. Ví dụ về sử dụng XmlDataDocument trong hiển thị dữ liệu từ Dataset DA = new SqlDataAdapter("SELECT * FROM Project", connectionString); DataSet DS = new DataSet("DMS"); DA.Fill(DS, "Project"); XmlDataDocument xmlDataDoc = new XmlDataDocument(DS); XmlControl.Document = xmlDataDoc; XmlControl.TransformSource = Server.MapPath("Projects.xslt"); Nội dung của file Project.xslt được dùng để hiển thị dữ liệu lên HTML <?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="DMS"> <h1>The Project List</h1> <xsl:apply-templates select="Project"/> <i>Created through XML and XSLT</i> </xsl:template> <xsl:template match="Project"> <p> <b>Name: </b><xsl:value-of select="Name"/> <b>Project Leader: </b><xsl:value-of select="ProjectLeaderID"/> <b>Estimate Effor: </b><xsl:value-of select="EstimateEffort"/> Biên tập: thienthanit@yahoo.com Nguồn: Internet <b>Project status: </b><xsl:value-of select="ProjectStatusID"/> </p> </xsl:template> </xsl:stylesheet> File XML sử dụng trong ví dụ DVDList.xml . tìm hiểu về cách bind dữ liệu XML vào một control hiển thị dữ liệu như GridView. Để thực hiện việc này, đầu tiên ta sẽ sử dụng control XmlDataSource quản lý dữ liệu từ file XML 1. Binding ở. DS.WriteXml(xmlFile,XmlWriteMode.WriteSchema);  ReadXml(): Tải dữ liệu từ xml file, TextReader hay XmlReader vào Dataset  DataSet DS = new DataSet("DMS"); DS.ReadXml(xmlFileName);. các thao tác liên quan tới XML và Dataset. WriteXml() và ReadXml() là 2 phương thức cơ bản nhất  WriteXml(): Ghi dữ liệu từ Dataset vào xml file, TextWriter hay XmlWriter.  SqlDataAdapter

Ngày đăng: 12/07/2014, 19:20