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

Receiving Plain XML Messages with Silverlight

Introduction

This QuickStart sample demonstrates how to use the HTTP transport to 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, such as a Web browser, 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 requested as strings by using the DownloadStringAsync(Uri) method of the WebClient class. Received POX messages are processed by handling the DownloadStringCompleted event and retrieving the Result property of the DownloadStringCompletedEventArgs. For more information, see the WebClient class documentation on MSDN.

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. For more information, see How to: Make a Service Available Across Domain Boundaries on MSDN.

This example shows you how to access an established Web service by 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. When you create a Silverlight-based application that communicates with a Web service, the Silverlight control must be hosted in a Web page that is served from a Web server. Attempts to communicate with a Web service from the file system (from a HTML test page, for example) causes a security exception.

The following steps show you how to access a Web service that returns results as XML.

  1. Create a Silverlight project. Make sure that you select the default option: Add a new Web to the Solution for hosting the control. If you choose the Dynamically generate an HTML test page to host Silverlight within this project option instead, you will receive a security exception when you run the sample. For more information, see Creating an Application for Silverlight.

  2. In the Silverlight project, make sure that you have a reference to the System.Net assembly. Also, in the Page.xaml code-behind file, make sure that you have a using statement to the System.Net namespace. You will need classes in this assembly to call a Web service.

  3. To call the Web service, create a WebClient and call its DownloadStringAsync(Uri) method. Pass the URI to the Web service you want to call as a parameter to the method.

  4. To retrieve the results of the Web service call, handle the DownloadStringCompleted event for the Web client. In the event handler, use the Result property of the DownloadStringCompletedEventArgs to get the results of the Web service call. You can either do additional processing with the results or just display them to the user. Use the Error property of the event arguments to determine whether an error has occurred with the Web service call.

The following example shows how to send a request to the Digg Web service and get a response back as XML. When you enter a number in the text box and click the button, the returned XML is displayed. Even though the Digg Web service is on a different domain, this request works because the Digg Web service allows cross-domain calls.

CS

<UserControl x:Class="POXQS.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    FontFamily="Trebuchet MS" FontSize="11"
    Width="500" Height="600">
    <Border BorderBrush="Black" BorderThickness="5" >
        <StackPanel Orientation="Vertical">
            <TextBlock  Margin="5" Text="POX Quickstart Example" />
             <StackPanel Orientation="Horizontal" Margin="5">
                <TextBlock VerticalAlignment="Center" 
                   Text="Enter a number in the box: (1-4) " />
                <TextBox x:Name="numTextBox" Width="20" />
             </StackPanel>
            <Button Width="200" HorizontalAlignment="Left" Margin="5"
              Content="Click for response from web service" 
                Click="Button_Click" />
            <TextBlock x:Name="resultBlock" Grid.Row="3" TextWrapping="Wrap" />
        </StackPanel>
    </Border>
</UserControl>


...
    public partial class Page : UserControl
    {

        WebClient client = new WebClient();

        public Page()
        {
            InitializeComponent();
            client.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }



        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string url1 = "http://services.digg.com/galleryphotos?count=" +
                numTextBox.Text +
                "&appkey=http%3A%2F%2Fwww.silverlight.net";

            client.DownloadStringAsync(new Uri(url1));
        }
        void client_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                resultBlock.Text = e.Result;
            }

            else
                resultBlock.Text = e.Error.Message;
        }
    }

Run View
Language:
Page view counter