Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

1098 Chapter 25 XML WEB SERVICES

Dim Prods As DataTable = localDS.Tables(“Products”) Dim Cats As DataTable = localDS.Tables(“Categories”)

Dim CatProds As New DataRelation(“CategoriesProducts”, _ Cats.Columns(“CategoryID”), Prods.Columns(“CategoryID”))

localDS.Relations.Add(CatProds) DataGrid1.DataSource = localDS

End Sub

The code first reads the data returned by the GetProducts method into a local DataSet. Then it establishes a relationship between the two tables based on the field CategoryID, which is common to both tables. The code then binds the DataGrid control to the DataSet by assigning the localDS variable to the control’s DataSource property. If you want to bind the grid to a single table, set the control’s DataMember property to the name of the appropriate table.

The second button on the form does something even more useful: it persists the data to a local file in XML format. The XML file can be used in another project, or move the data to a different database. Just add the file to the project (with the Add Existing Item command of the project’s context menu). To persist the data to a file, the Save Data button calls the WriteXML method of the DataSet object:

Private Sub Button2_Click_1(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click

localDS.WriteXML(“c:\Products.xml”) End Sub

Summary

Visual Studio .NET allows you to build Web services with the same tools as for regular classes that expose methods. You’ve seen how easy it is to build Web services and how to consume them from within Web and Windows apps. The client applications contact a URL, which consists of the name of the server where the Web service is running, followed by the name of the service and the name of a method. The method returns its result in XML format, and you can use it in your code as if it were the result of a function call.

You’re already seeing the value of XML. Throughout the book, I’ve tried not to shift the focus from VB and the visual tools to XML. Ideally, XML should be made transparent to the developer, and as you have seen, you don’t need to know XML to create DataSets or XML Web services. It’s good to know that there’s a format that makes it all possible, but why should we have to get down to the details of that protocol? There are tools to manipulate XML files, but I’ve decided that the language, the visual tools, and the framework are plenty to keep you busy for a long time. Once you develop a solid understanding of .NET, you can move into XML and enjoy it.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com