Xem mẫu

XML 313 If needed, the entire path to the .dtd file can be specified—for example, C:\ModelingFM\xml\fmml.dtd. If the DTD is public as in the FMML case, meaning that it is available on the Internet, the syntax will be: When a DTD exists as a URI, it becomes especially powerful. Assuming that all partners who exchange XML messages can access the DTD, they all can use it to create and validate their XML documents anywhere in the world. Let’s take a look. CREATING XML DOCUMENTS Before we create an Internet application, let’s first develop a simple VB.NETapplicationthat will write an XML document and save it to the C:\drive. Step 1 In VB.NETcreate a new Windows application named XMLexample. Step 2 On your Form1, add controls to build the GUI shown in Figure 18.2. There should be two combo boxes on your form. Name them cboExchange and cboBuySell. In the Collection property of cboExchange, add the elements CBOE, ISE, BOX, AMEX, and FMEX. In the Collection property of cboBuySell, add the elements Buy and Sell. Give the text boxes the appropriate names: txtTicker, txtQuantity, txtPrice, txtClearingFirm, and txtTrader. Step 3 To the Button1_Click event, add the following code: Imports System.IO Public Class Form1 Inherits System.Windows.Forms.Form [Windows Form Designer Generated Code] Private Sub Button1_Click(ByVal sender As ...) Handles Button1.Click Dim strTradeDoc As String strXMLtrade = "" strXMLtrade &= "" strXMLtrade &="" strXMLtrade &= "" strXMLtrade &= "" & txtTicker.Text & "" Team-LRN 314 Advanced VB.NET F I G U R E 18.2 strXMLtrade &= "" strXMLtrade &= "" & txtQuantity.Text & "" strXMLtrade &= "" strXMLtrade &= "" & txtClearingFirm.Text & "" strXMLtrade &= "" & txtTrader.Text & "" strXMLtrade &= "" strXMLtrade &= "" strXMLtrade &= "" Dim objWriter As New StreamWriter("C:\ModelingFM\myFirstXMLdoc.xml") objWriter.Write(strXMLtrade) objWriter.Close() End Sub End Class Step 4 Step 5 Run the program. Your results should look like the screen shown in Figure 18.3. Now find the file named myFirstXMLdoc.xml in your C:\ModelingFM folder and double-click on it, which will cause it to open in MS Internet Explorer. Team-LRN XML 315 F I G U R E 18.3 If your XML document is well formed, it will successfully open. If it is not well formed, Internet Explorer (IE) will generate an error statement. SENDING XML MESSAGES Creating an XML message and saving it to a file is not really all that exciting. After all, XML was designed for communication. Let’s change our program so we can send our FMML trade over the Internet and receive a trade confirmation. We will be sending our pseudo trades to the Financial Markets Exchange,FMEX, which is a server that will receive FMML “trades,” post them in a database, and send back FMML trade confirmations. Once you have placed your trade, you can see whether the FMEX has received it at http:// yorkville.rice.iit.edu:8100/servlet/fmex.GetTrades. This website shows Team-LRN 316 Advanced VB.NET the contents of the FMEX database. So if your FMML document was successfully received, it will be viewable on this site. Step 6 To communicate with the FMEX over the Internet, we will need to create a few objects that are based upon classes found in the System.Net and System.XML namespaces. Add the Imports System.Net and Imports System.XML code at the very top of the Form1 code window. In the general declarations section of the Form1 code window, declare the following objects: Dim myUrl As Uri Dim myReq As WebRequest Dim myRes As WebResponse Dim myReader As XmlTextReader A URI is an object representation of a URL. A WebRequest object, found in the System.Net namespace, makes a request to a URI over theInternet. AWebResponseobjectsreceivesa messagefrom aURI. An XmlTextReader object, found in the System.XML name-space, gives us fast, read-only access to an XML stream. So by using an XMLTextReader to read a stream, we will be implementing a form of SAX parser to make sure the XML message is well formed. However, an XmlTextReader object will not perform data validation against a DTD. To perform data validation, we could use an XmlValidatingReader object, but creating a full, validating parser is beyond the scope of this chapter. Step 7 Change the Button1_Click event to include the following code: Private Sub Button1_Click(ByVal sender As ...) Handles Button1.Click Dim strCurrentTag, strStatus, strBuySell, strQty, strTicker, _ strPrice, strDate, strXMLtrade As String strXMLtrade = "" strXMLtrade += " " strXMLtrade += "" ’ This code is the same as before. strXMLtrade += "" myUrl = New _ Uri("http://yorkville.rice.iit.edu:8100/servlet/fmex.XMLTrade?xmlfile=" _ & strXMLtrade) myReq = WebRequest.Create(myUrl) Team-LRN XML 317 Try myRes = myReq.GetResponse myReader = New XmlTextReader(myRes.GetResponseStream()) Do While (myReader.Read()) If myReader.NodeType = XmlNodeType.Element Then strCurrentTag = myReader.Name End If If myReader.NodeType = XmlNodeType.Text Then Select Case strCurrentTag Case "Status" strStatus = myReader.Value Case "BuySell" strBuySell = myReader.Value Case "Quantity" strQty = myReader.Value Case "Ticker" strTicker = myReader.Value Case "Price" strPrice = myReader.Value Case "Time" strDate = myReader.Value End Select End If Loop myReader.Close() Catch exc As Exception MsgBox(exc.Message) Exit Sub End Try lblConfirm.Text = strStatus & " " & strBuySell & " " & strQty & " _ " & strTicker & " at " & strPrice & " at " & strDate End Sub Step 8 One last thing: Add a label named lblConfirm to the bottom of your Form1. Step 9 Run the program (see Figure 18.4). You can view the FMEX server at http://yorkville.rice.ii-t.edu:8100/servlet/fmex.GetTrades. When the server receives your “trade,” it will be posted on this website. XML DATA SOURCES ADO.NET provides additional functionality that allows us to convert data in a database into XML. VB.NET DataSet objects provide methods that create XML documents from a data table and that also can convert XML data into a data source. This is accomplished through the use of the GetXML(), WriteXML, and ReadXML() member methods. Let’s look at a quick example: Team-LRN ... - tailieumienphi.vn
nguon tai.lieu . vn