13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

L<strong>in</strong>k<strong>in</strong>g Tables and ViewsThe l<strong>in</strong>k between the DataTable object and the DataView object is typically establishedat creation time through the constructor, as shown here:public DataView(DataTable table);However, you could also create a new view and associate it with a table at a later timeus<strong>in</strong>g the DataView object's Table property, as <strong>in</strong> the follow<strong>in</strong>g example:DataView dv = new DataView();dv.Table = dataSet.Tables["Employees"];You can also obta<strong>in</strong> a DataView object from any table. In fact, the DefaultView propertyof a DataTable object simply returns a DataView object <strong>in</strong>itialized to work on that table,as shown here:DataView dv = dt.DefaultView;Orig<strong>in</strong>ally, the view is unfiltered, and the <strong>in</strong>dex array conta<strong>in</strong>s as many elements asthere are rows <strong>in</strong> the table.Gett<strong>in</strong>g Views of RowsThe contents of a DataView object can be scrolled through a variety of programm<strong>in</strong>g<strong>in</strong>terfaces, <strong>in</strong>clud<strong>in</strong>g collections, lists, and enumerators. The GetEnumerator method <strong>in</strong>particular ensures that you can walk your way through the records <strong>in</strong> the view us<strong>in</strong>g thefamiliar <strong>for</strong>each statement.The follow<strong>in</strong>g code shows how to access all the rows that fit <strong>in</strong>to the view:DataView myView = new DataView(table);<strong>for</strong>each(DataRowView rowview <strong>in</strong> myView){// Dereferences the DataRow objectDataRow row = rowview.Row;}⋮When client applications access a particular row <strong>in</strong> the view, the class expects to f<strong>in</strong>d it<strong>in</strong> an <strong>in</strong>ternal rows cache. If the rows cache is not empty, the specified row is returnedto the caller via an <strong>in</strong>termediate DataRowView object. The DataRowView object is awrapper <strong>for</strong> the DataRow object that conta<strong>in</strong>s the actual data. You access row datathrough the Row property. If the rows cache is empty, the DataView class fills it with anarray of DataRowView objects, each of which references an orig<strong>in</strong>al DataRow object.The rows cache can be empty either because it has not yet been used or because thesort expression or the filter str<strong>in</strong>g has been changed <strong>in</strong> the meantime.Serializ<strong>in</strong>g DataView ObjectsThe AdoNetXmlSerializer class also provides overloaded methods to serialize aDataView object. You build a copy of the orig<strong>in</strong>al DataTable object with all the rows(and only those rows) that match the view, as shown here:public staticvoid WriteDataView(DataView dv, str<strong>in</strong>g outputFile, XmlWriteModemode)341

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!