Binding to XML Data From the Web

The Data Access API, defined in the Microsoft.MediaCenter.DataAccess namespace, allows Windows Media Center applications to request XML data from RESTful web services within MCML. This allows the application to bind a view item in MCML to XML data (a remote resource) from the web.

  • A Uniform Resource Identifier (URI) specifies the location of the resource for the data request, which returns a response document.
  • An XPath query string is used to get data within the XML response document and map it to a view item. Because XML is not typed, mappings are used to specify the data type of the XML source data so that it can be bound to the view item.

Process Overview

The basic process is as follows.

  1. In the Properties section of the UI:

    • Specify the URI to the resource using the RemoteResourceUri class.
    • Set up the remote resource using the RemoteResource class.
    • Set up the mappings for the different remote values in the remote resource using the RemoteValue class.
    • For remote values of the same type (repeated data), you can create a remote value list using the RemoteValueList class. A remote value list contains the repeated remote values and possibly additional lists.

    The following example shows the setup for a remote resource:

    <Properties>
      <da:RemoteResourceUri Name="MyUri" />
      <da:XmlRemoteResource Name="MyResource">
        <Mappings>
          <da:XmlRemoteValue Name="Artist"
                             Type="cor:String"
                             Source="/album/@artist" />
          <da:XmlRemoteValue Name="Title"
                             Type="cor:String"
                             Source="/album/@title" />
          <da:XmlRemoteValueList Name="Tracks"
                                 RepeatedType="PropertySet"
                                 Source="/album/tracks/track" >
            <Mappings>
              <da:XmlRemoteValue Property="Entries.#Title"
                                 Type="cor:String"
                                 Source="/@title" />
              <da:XmlRemoteValue Property="Entries.#Art"
                                 Type="cor:String"
                                 Source="/@art" />
            </Mappings>
          </da:XmlRemoteValueList>
        </Mappings>
      </da:XmlRemoteResource>
    </Properties>
    

    Note   For XML-specific parsing and mapping, applications can use the following classes:

  2. Initiate the XML data request in the Rules section using the RemoteResource.GetDataFromResource method. For example:

    <Rules>
        <Changed Source="[MyCommand.Invoked]">
            <Actions>
                <Invoke Target="[MyResource.GetDataFromResource]" InvokePolicy="Synchronous" />
            </Actions>
          </Changed>
    </Rules>
    
  3. The RemoteResource object processes the response and parses the data that is received.

    If a mapping is incorrect (for example, the resource does not exist), an error message is not displayed and the the value of the resource will simply appear empty.

    If the server's certificate is not valid, an ErrorResponse error is returned. In a web browser, the user can continue past an invalid or expired certificate error, but the data access model item API does not automatically continue past this warning, and the choice is not given to the user or to the calling application. 

  4. The application can set up rules to bind the source XML data to a target. The following example shows the correct syntax.

    Note   Although the data type was already mapped in the Properties section, it must be cast again.

    <Binding Target="[MyArtist.PropertyValue]"
        Source="[MyResource.Mappings.#Artist!da:XmlRemoteValue.String]" >
    
  5. The application can query several properties in the RemoteResource object for information about the request, such as whether the request is complete or has changed. Query the ResponseStatusCode and ResponseStatusDescription properties within a Changed rule for status information.

Note    If subsequent requests are made that contain null or invalid data, any previous values are not cleared but are used until a successful request and response is made. This allows the UI to continue to work with valid data.

For more information about media collections, see the webDataBasic.mcml and webDataBasicData*.xml samples in the Sample Explorer.

Sample Explorer

  • Web Data > WebDataBasic.mcml
  • Web Data > WebDataResponseDocument.mcml

See Also