1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft ADO .NET 4 Step by Step - p 15 doc

10 294 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 499,47 KB

Nội dung

116 Microsoft ADO.NET 4 Step by Step Generating XML from a DataSet : Visual Basic 1. Open the “Chapter 7 VB” project from the installed samples folder. The project includes one Windows.Forms class named Serialization. 2. Open the source code view for the Serialization form. Locate the ActGenerate_Click function. This routine produces the XML content from a sample DataSet containing two tables: Customer and Order. 3. Just after the “Set the XML namespace” comment, add the following statements: SampleDataSet.Tables("Customer").Namespace = TableNamespace.Text.Trim SampleDataSet.Tables("Customer").Prefix = TablePrefix.Text.Trim SampleDataSet.Tables("Order").Namespace = TableNamespace.Text.Trim SampleDataSet.Tables("Order").Prefix = TablePrefix.Text.Trim This code sets the namespace and prefix values for both of the sample tables. Note As mentioned in the chapter discussion, you can also define namespace and prefix val- ues within each D ataColumn. Although it is not included in the sample code, consider adding code that will loop through all columns in each of the two tables and add the user-specified namespace and prefix values. 4. Just after the “Indicate the relationship type” comment, add the following line: SampleDataSet.Relations(0).Nested = NestChildRecords.Checked This statement determines whether the order rows for each customer record are con- tained within the <Customer> tag (True) or whether all <Order> tags appear after and at the same level as all the <Customer> tags in the XML (False). 5. Just after the “Build a memory stream to hold the results” comment, add the following code: holdBuffer = New MemoryStream(8192) SampleDataSet.WriteXml(holdBuffer, CType(OutputWriteMode.SelectedItem, XmlWriteMode)) These lines perform the actual XML generation, sending the results to a stream, in this case a MemoryStream instance. The remaining code in the event handler moves the XML content from the stream to an on-form text box. Chapter 7 Saving and Restoring Data 117 6. Run the program. Use the fields in the upper-right corner of the form to alter the XML content and then click Generate to produce the XML. As an example, set the XML Write Mode to IgnoreSchema; select the Nest Child Records check box; change the Mapping for Child.ID to Attribute; change the Mapping for Child.CustomerID, Child. OrderDate, Child.Subtotal, and Child.TaxRate to Hidden; and finally change the Mapping for Child.Total to SimpleContent. Click Generate. The XML will contain a simple set of customer records, each containing one or more <Order> tags with an ID attribute, and with the order total set as the element content. Summary This chapter introduced the XML-specific features built into ADO.NET classes. These features exist primarily to assist in serializing static XML content for disk-based storage or for transfer to other applications that expect ADO.NET-generated XML content. The XML produced by these classes can define its own schema using embedded or external XSD and can build hier- archical XML elements based on the relationships in the original DataSet. There are other ways to bring ADO.NET data and XML together in your applications. LINQ, a major language feature in both Visual Basic and C#, includes data-querying features for both ADO.NET and XML, features that can work in tandem. Chapters 17 through 20 in this book discuss various LINQ-related technologies. Although “LINQ to XML” is not specifically exam- ined in this book, the general LINQ concepts outlined in those chapters are similar to those used when writing LINQ queries for XML data. 118 Microsoft ADO.NET 4 Step by Step Chapter 7 Quick Reference To Do This Export a DataSet to a file as XML Create a DataSet instance. Add all relevant DataTable, DataRelation, and content objects. Call the WriteXml method of the DataSet, passing it the file name as an argument. Import file-based XML into a new DataSet Create a new DataSet instance. Call the ReadXml method of the DataSet, passing it the file name as an argument. Generate hierarchical parent-child data Create a DataSet instance. Add the relevant DataTable objects. Add a DataRelation instance that links the tables. Set the DataRelation.Nested property to True. Call DataSet.WriteXml to generate the XML content. Store a DataColumn as an XML-based attribute Set the DataColumn.ColumnMapping property to MappingType. Attribute. Microsoft ADO.NET 4 Step by Step 119 Part II Connecting to External Data Sources Chapter 8: Establishing External Connections Chapter 9: Querying Databases Chapter 10: Adding Parameters to Queries Chapter 11: Making External Data Available Locally Chapter 12: Guaranteeing Data Integrity . as an XML-based attribute Set the DataColumn.ColumnMapping property to MappingType. Attribute. Microsoft ADO. NET 4 Step by Step 119 Part II Connecting to External Data Sources Chapter 8: Establishing. simplify application use reporting.  The AttachDBFilename key lets you attach a SQL Server Express Edition .mdf data file by referring to its filename. 1 24 Microsoft ADO. NET 4 Step by Step  The. 116 Microsoft ADO. NET 4 Step by Step Generating XML from a DataSet : Visual Basic 1. Open the “Chapter 7 VB” project from the installed samples folder. The project includes one

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

TỪ KHÓA LIÊN QUAN