Working with XML in the Client


Last but not least, the information that your client is returning might be only part of the story. In Chapter 6, you saw examples in which we needed the XML itself from the client, not the actual objects. We covered the XML APIs in earlier chapters, so here we’ll focus instead on manipulating the XML we receive using XSLT transformations to help create our interface.

With our weather reporting Web service, you’ve seen how we can return the XML and mold it into a roughly presentable format. However, we can use XSLT to improve the interface even more.

Using XSLT for User Interface Purposes

To use XSLT in the client, you send a source XML document and an XSLT style sheet to the browser and get the browser to apply the XSLT transformation for you. One problem is that only the most recent browsers (such as Internet Explorer 6.0 and later, Netscape 6.0 and later, and Mozilla) support the XSLT 1.0 standard. We’ll assume that you’re using one of these browsers.

In the Web Forms example, we supply the following code for button 1’s event handler:

  using System.Xml.Xsl; using System.Xml; using System.Xml.XPath;  private void Button1_Click(object sender, System.EventArgs e) {     XmlDocument doc = new XmlDocument();     doc.LoadXml(USW.GetWeatherReport(TextBox1.Text));     doc.Save("C:\\test.xml");     XPathDocument doct = new XPathDocument("C:\\test.xml");     XslTransform xslt = new XslTransform();      xslt.Load("C:\\xsltfile.xslt");     XmlReader xmlReader = xslt.Transform(doct,null);     xmlReader.MoveToContent();     this.Label2.Text = xmlReader.ReadOuterXml();     xmlReader.Close(); }

Next we create a new XSLT file with the following code, which should be saved as xsltfile.xslt to an appropriate location. (We’ve used the root C:\ folder for simplicity.)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">     <xsl:output method="html" indent="no"/>     <xsl:template match="/">         <html>             <body>                 <xsl:apply-templates />             </body>         </html>     </xsl:template>     <xsl:template match="Weather">         <table bgcolor="efefef" cellpadding="5" width="640">             <tr>                 <td>                     City:                         <b>                             <xsl:value-of select="City" />                         </b>                 </td>                 <td>                     State:                      <xsl:value-of select="State" />                 </td>                 <td>                     County:                      <xsl:value-of select="County" />                 </td>             </tr>             <tr>                 <td>Fahrenheit:                     <xsl:value-of select="Fahrenheit" />                 </td>                 <td>Celsius:                     <xsl:value-of select="Celsius" />                 </td>                 <td>Condition:                     <xsl:value-of select="Condition" />                 </td>                         </tr>             <tr>                 <td>Humidity:                     <xsl:value-of select="Humidity" />                 </td>                 <td>Wind:                     <xsl:value-of select="Wind" />                 </td>                 <td>Sunrise/Sunset:                      <xsl:value-of select="Sunrise" />/                     <xsl:value-of select="Sunset" />                 </td>             </tr>         </table>     </xsl:template> </xsl:stylesheet>

When you run the application now, you get the result shown in Figure 8-16.

click to expand
Figure 8-16: Web service client application using XSLT

We get this result because our transformation has been applied to the XML returned by the Web service. In our code, we started by loading the response from our XMLDocument object. From there we saved it into an actual file:

XmlDocument doc = new XmlDocument(); doc.LoadXml(USW.GetWeatherReport(TextBox1.Text)); doc.Save("C:\\test.xml");

This is a fairly crude way to get an XPathDocument object to accept the XML format returned by the Web service. As noted earlier, the document is returned within a string element, but loading it into an XMLDocument object and saving it breaks it out of this element. Next we load the document into the XPathDocument object, which is passed as a parameter to the Transform method, which performs the simple transformation on the XML file.

    XPathDocument doct = new XPathDocument("C:\\test.xml");

We then create an XslTransform object and load our XSLT style sheet into it:

    XslTransform xslt = new XslTransform();      xslt.Load("C:\\xsltfile.xslt");

Next we create an XmlReader object and read results of our transformation into it. Finally, we display contents of the XmlReader object in our label control:

    XmlReader xmlReader = xslt.Transform(doct,null);     xmlReader.MoveToContent();     this.Label2.Text = xmlReader.ReadOuterXml();     xmlReader.Close();

This code returns the results to the user.

Using XSLT for Noninterface Purposes

You can also use XSLT to alter parts of the client other than the interface. For instance, you can use XSLT for versioning, such as when a new version of a Web service produces a response format that is different from the old version of the Web service. We looked at this subject in the previous chapter.




Programming Microsoft. NET XML Web Services
Programming MicrosoftВ® .NET XML Web Services (Pro-Developer)
ISBN: 0735619123
EAN: 2147483647
Year: 2005
Pages: 172

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net