[This topic is pre-release documentation and is subject to change in future releases of Microsoft Silverlight.]

Sending and Receiving Plain XML Messages with Silverlight

Introduction

This QuickStart sample demonstrates how to use the HTTP transport to send and receive plain XML messages (sometimes referred to as Plain Old XML or POX messages).

POX messages consist only of XML payloads without an enclosing SOAP envelope. POX messages can be sent and received by many types of clients that include Web browsers that do not have native support for SOAP-based protocols. POX is an appropriate choice for services that exchange data over HTTP and that are not required to use the advanced capabilities of SOAP and WS-* protocols. POX messages are sent by using the DownloadStringAsync(Uri) method of the WebClient class. POX messages are received by handling the DownloadStringCompleted event and retrieving the Result property of the DownloadStringCompletedEventArgs.

note

The WebClient class does not currently support cross-domain calls. The current release of WebClient requires that the Silverlight-based application be hosted on the same server as the target Web service. Alternatively, if the Web service and Silverlight-based application are hosted in separate domains, the Web service hosting the service must have an XML policy file that indicates that clients can make these cross-domain calls.

In this example, you send an XML message to the Digg Web service by using the defined XML policy. This Web service returns the specified number of images last added to the image gallery. This example shows you how to access an established Web service using the WebClient class. You can also create your own Web services and access them from your Silverlight-based applications, as long as both the service and application are hosted in the same domain.

Run View
Language:

Working with POX in your Silverlight-based application involves the following steps:

  • Creating a Silverlight project.

  • Adding managed code to connect to an existing Web service asynchronously.

Prerequisites (available from the Silverlight download site):

  • Microsoft Visual Studio 2008

  • Silverlight version 2 Beta 1

  • Silverlight 2 Beta 1 SDK

  • Silverlight Tools Beta 1 for Visual Studio 2008

To create a Silverlight project

  1. In Microsoft Visual Studio 2008, create a new Siliverlight Application in Visual C#.

  2. In the Add Silverlight Application dialog box, make sure that you select the default option: Add a new Web to the Solution for hosting the control. If you choose the Generate an HTML test page to host Silverlight within this project option instead, you might receive a security exception when you run the sample.

    For more information, see Creating an Application for Silverlight.

To call the Web service asynchronously

  1. In XAML view for Page.xaml, change the width and the height for the Silverlight control to 600.

    Width="600" Height="600"
  2. In XAML view for Page.xaml, add the following XAML to create the UI for the Silverlight control.

    CS

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Width="250" 
    HorizontalAlignment="Left" 
    Content="Click for response from gallery service" Click="Button_Click" />
    <TextBlock HorizontalAlignment="Left" Grid.Row="2" Text="Enter a number in the box: (1-4) " />
    <TextBox x:Name="numTextBox"  Grid.Row="2" Width="20" />
    <TextBlock x:Name="resultBlock" Grid.Row="3"/>
    
    
  3. To generate the button click event handler, right-click the button Click attribute and select Navigate to Event Handler from the context menu.

  4. In the event handler for the button click event, construct the request URI by using input from the user to determine how many gallery images to retrieve.

    CS

    string url1 = "http://services.digg.com/galleryphotos?count=" + 
        numTextBox.Text + 
        "&appkey=http%3A%2F%2Fwww.silverlight.net";
    
    
  5. Create a WebClient object and associate a handler for its DownloadStringCompleted event. Next, call the DownloadStringAsync(Uri) method, passing the URL constructed in the previous step.

    CS

    WebClient client = new WebClient();
    
    client.DownloadStringCompleted += 
        new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    client.DownloadStringAsync(new Uri(url1));
    
    
  6. Next, add the handler for the DownloadStringCompleted event, which processes the response. In this example, the response displays in a text box on the Silverlight control.

    CS

    void client_DownloadStringCompleted(object sender, 
        DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            resultBlock.Text = e.Result;
        }
    
        else
            resultBlock.Text = e.Error.Message;
    }
    
    
  7. Finally, add a using statement for the System.Net namespace.

  8. When you run the sample, enter a number, and click the button, XML is retrieved from the Digg Web service.