/// <summary>
        /// Creates a new HealthRecordSearcher for a list of specific types.
        /// </summary>
        /// 
        /// <returns>
        /// A <see cref="HealthRecordSearcher"/> that searches for items with specific type IDs
        /// within this record.
        /// </returns>
        /// 
        /// <remarks>
        /// The method adds a filter to the <see cref="HealthRecordSearcher"/> that only returns
        /// items of the specified type IDs. That filter may be accessed through the
        /// returned searcher using searcher.Filters[0].
        /// 
        /// You can also call the <see cref="HealthRecordSearcher"/> constructor 
        /// directly and pass in a reference to this 
        /// <see cref="HealthRecordAccessor"/>.
        /// </remarks>
        /// 
        /// <param name="typeIds">
        /// A list of unique type ids to filter on.
        /// </param>
        ///
        public HealthRecordSearcher CreateSearcher(params Guid[] typeIds)
        {
            HealthRecordSearcher searcher = new HealthRecordSearcher(this);
            HealthRecordFilter filter = new HealthRecordFilter(typeIds);
            searcher.Filters.Add(filter);

            return searcher;
        }
        /// <summary>
        /// </summary>
        /// 
        /// <returns>
        /// </returns>
        /// 
        /// <remarks>
        /// This method accesses the HealthVault service across the network.
        /// If the filters specified do not reduce the amount of data being retrieved from the
        /// record, this method could take a significant amount of time as data gets paged into
        /// memory from HealthVault.
        /// 
        /// Note: There may be data in items that HealthVault does not include when converting 
        /// to the CCR or CCD formats.
        /// </remarks>
        /// 
        /// <exception cref="HealthServiceException">
        /// The response from the server was anything but 
        /// <see cref="HealthServiceStatusCode.Ok"/>.
        /// -or-
        /// <see cref="Microsoft.Health.HealthRecordSearcher.Filters"/> is empty
        /// or contains invalid filters.
        /// </exception>
        /// 
        public string ExportItems()
        {
            HealthRecordSearcher searcher = new HealthRecordSearcher(_record);

            for (int index = 0; index < Filters.Count; ++index)
            {
                searcher.Filters.Add(Filters[index]);
            }

            ReadOnlyCollection<HealthRecordItemCollection> results = searcher.GetMatchingItems();

            StringBuilder resultXml = new StringBuilder(1000);
            XmlWriterSettings settings = SDKHelper.XmlUnicodeWriterSettings;
            settings.ConformanceLevel = ConformanceLevel.Document;

            using (XmlWriter writer = XmlWriter.Create(resultXml, settings))
            {
                // <response>
                writer.WriteStartElement("response");

                // <wc:info>
                writer.WriteStartElement("wc", "info", "urn:com.microsoft.wc.methods.response.GetThings3");

                foreach (HealthRecordItemCollection items in results)
                {
                    // <group>
                    writer.WriteStartElement("group");

                    // We are leveraging the HealthRecordItemCollection paging here to retrieve
                    // all the data from HealthVault if it doesn't come with the first request.
                    for (int itemIndex = 0; itemIndex < items.Count; ++itemIndex)
                    {
                        bool filterApproved = true;
                        foreach (HealthRecordClientFilterHandler filter in ClientFilters)
                        {
                            filterApproved = filter(items[itemIndex]);
                            if (!filterApproved)
                            {
                                break;
                            }
                        }

                        if (filterApproved)
                        {
                            items[itemIndex].WriteItemXml(writer, true, "thing");
                        }
                    }

                    // </group>
                    writer.WriteEndElement();
                }

                // </wc:info>
                writer.WriteEndElement();

                // </response>
                writer.WriteEndElement();

                writer.Flush();
                writer.Close();
            }

            return TransformItemXml(resultXml.ToString());
        }
 /// <summary>
 /// Gets the health record items that match the filters as specified by 
 /// the properties of this class.
 /// </summary>
 /// 
 /// <param name="connection">
 /// The connection to use to access the data.
 /// </param>
 /// 
 /// <param name="accessor">
 /// The record to use.
 /// </param>
 /// 
 /// <param name="searcher">
 /// The searcher that defines what items to return.
 /// </param>
 /// 
 /// <returns>
 /// An XPathNavigator representing the raw results of the search.
 /// </returns>
 /// 
 /// <remarks>
 /// This method accesses the HealthVault service across the network.
 /// <br/><br/>
 /// This method is typically used when the calling application wants to
 /// handle the raw health record item XML directly instead of using the 
 /// object model.
 /// </remarks>
 /// 
 public static XPathNavigator GetMatchingItemsRaw(
     ApplicationConnection connection,
     HealthRecordAccessor accessor,
     HealthRecordSearcher searcher)
 {
     return HealthVaultPlatformItem.Current.GetMatchingItemsRaw(connection, accessor, searcher);
 }
 /// <summary>
 /// Gets the health record items that match the filters as specified by 
 /// the properties of this class.
 /// </summary>
 /// 
 /// <param name="connection">
 /// The connection to use to access the data.
 /// </param>
 /// 
 /// <param name="accessor">
 /// The record to use.
 /// </param>
 /// 
 /// <param name="searcher">
 /// The searcher that defines what items to return.
 /// </param>
 /// 
 /// <returns>
 /// An XmlReader representing the raw results of the search.
 /// </returns>
 /// 
 /// <remarks>
 /// This method accesses the HealthVault service across the network.
 /// <br/><br/>
 /// This method is typically used when the calling application wants to
 /// handle the raw health record item XML directly instead of using the 
 /// object model.
 /// </remarks>
 /// 
 public static XmlReader GetMatchingItemsReader(
     ApplicationConnection connection,
     HealthRecordAccessor accessor,
     HealthRecordSearcher searcher)
 {
     return HealthVaultPlatformItem.Current.GetMatchingItemsReader(connection, accessor, searcher);
 }
 /// <summary>
 /// Creates a new open query using the specified 
 /// connection, definition, expiration time,
 /// personal identification number (PIN), description, and XSL.
 /// </summary>
 /// 
 /// <param name="connection">
 /// An <see cref="ApplicationConnection"/> instance that creates the 
 /// new open query.
 /// </param>
 /// 
 /// <param name="searcher">
 /// A <see cref="HealthRecordSearcher"/> instance that defines the open query.
 /// </param>
 ///
 /// <param name="expires">
 /// The number of minutes the query will expire from the creation time.
 /// A value of Int32.MaxValue denotes that the query does not expire.
 /// </param>
 ///
 /// <param name="pinCode">
 /// The PIN that protects the query.  
 /// </param>
 ///
 /// <param name="note">
 /// The note describing the query.
 /// </param>
 /// 
 /// <param name="finalXsl">
 /// The XSL that transforms the results of the query when the 
 /// <see cref="OpenQuery"/> is invoked.
 /// </param>
 /// 
 /// <returns>
 /// An <see cref="OpenQuery"/> instance that represents the newly created query.
 /// </returns>
 /// 
 /// <remarks>
 /// The creation of an open query makes public the data returned by that 
 /// query. However, the query URL must be known to retrieve the data.
 /// </remarks>
 /// 
 /// <exception cref="ArgumentNullException">
 /// The <paramref name="connection"/> or <paramref name="searcher"/> 
 /// parameter is <b>null</b>.
 /// </exception>
 /// 
 /// 
 /// <exception cref="ArgumentException">
 /// The <paramref name="searcher"/> parameter contains no valid search
 /// filters or the <paramref name="pinCode"/> parameter is <b>null</b> 
 /// or empty.
 /// </exception>
 /// 
 /// <exception cref="HealthServiceException">
 /// An error occurred when HealthVault processed the request.
 /// </exception>
 /// 
 public static OpenQuery NewOpenQuery(
     ApplicationConnection connection,
     HealthRecordSearcher searcher,
     int expires,
     string pinCode,
     string note,
     string finalXsl)
 {
     return HealthVaultPlatformOpenQuery.Current.NewOpenQuery(
         connection,
         searcher,
         expires,
         pinCode,
         note,
         finalXsl);
 }
 /// <summary>
 /// Gets the health record items that match the filters as specified by 
 /// the properties of this class.
 /// </summary>
 /// 
 /// <param name="connection">
 /// The connection to use to access the data.
 /// </param>
 /// 
 /// <param name="accessor">
 /// The record to use.
 /// </param>
 /// 
 /// <param name="searcher">
 /// The searcher that defines what items to return. .
 /// </param>
 /// 
 /// <returns>
 /// A collection of health record items that match the applied filters.
 /// </returns>
 /// 
 /// <remarks>
 /// This method accesses the HealthVault service across the network.
 /// </remarks>
 /// 
 /// <exception cref="HealthServiceException">
 /// The response from the server was anything but 
 /// <see cref="HealthServiceStatusCode.Ok"/>.
 /// -or-
 /// <see cref="Microsoft.Health.HealthRecordSearcher.Filters"/> is empty
 /// or contains invalid filters.
 /// </exception>
 /// 
 public static ReadOnlyCollection<HealthRecordItemCollection> GetMatchingItems(
     ApplicationConnection connection,
     HealthRecordAccessor accessor,
     HealthRecordSearcher searcher)
 {
     return HealthVaultPlatformItem.Current.GetMatchingItems(connection, accessor, searcher);
 }
 /// <summary>
 /// Gets the health record items specified by the 
 /// <see cref="HealthRecordSearcher"/> and runs them through the specified 
 /// transform.
 /// </summary>
 /// 
 /// <param name="connection">
 /// The connection to use to access the data.
 /// </param>
 /// 
 /// <param name="accessor">
 /// The record to use.
 /// </param>
 /// 
 /// <param name="searcher">
 /// The searcher that defines what items to return.
 /// </param>
 /// 
 /// <param name="transform">
 /// A URL to a transform to run on the resulting XML. This can be
 /// a fully-qualified URL or the name of one of the standard XSLs
 /// provided by the HealthVault system.
 /// </param>
 /// 
 /// <returns>
 /// The string resulting from performing the specified transform on
 /// the XML representation of the items.
 /// </returns>
 /// 
 /// <remarks>
 /// This method accesses the HealthVault service across the network.
 /// <br/><br/>
 /// Any call to HealthVault may specify a transform to be run on the
 /// response XML. The transform can be specified as a XSL fragment or
 /// a well-known transform tag provided by the HealthVault service. If a
 /// XSL fragment is specified, it gets compiled and cached on the server.
 /// <br/>
 /// <br/>
 /// A final-xsl is useful when you want to convert the result from XML to
 /// HTML so that you can display the result directly in a web page.
 /// You may also use it to generate other data formats like CCR, CCD, CSV,
 /// RSS, etc.
 /// <br/>
 /// <br/>
 /// Transform fragments cannot contain embedded script. The following set
 /// of parameters are passed to all final-xsl transforms:<br/>
 /// <ul>
 ///     <li>currentDateTimeUtc - the date and time just before the transform 
 ///     started executing</li>
 ///     <li>requestingApplicationName - the name of the application that
 ///     made the request to HealthVault.</li>
 ///     <li>countryCode - the ISO 3166 country code from the request.</li>
 ///     <li>languageCode - the ISO 639-1 language code from the request.</li>
 ///     <li>personName - the name of the person making the request.</li>
 ///     <li>recordName - if the request identified a HealthVault record to 
 ///     be used, this parameter contains the name of that record.</li>
 /// </ul>
 /// </remarks>
 /// 
 /// <exception cref="ArgumentException">
 /// The <paramref name="transform"/> parameter is <b>null</b> or empty.
 /// </exception>
 /// 
 /// <exception cref="ArgumentException">
 /// <see cref="Microsoft.Health.HealthRecordView.Sections"/> does not
 /// contain the XML section in the view.
 /// </exception>
 /// 
 /// <exception cref="HealthServiceException">
 /// There is a failure retrieving the items.
 /// -or-
 /// No filters have been specified.
 /// </exception>
 /// 
 public static string GetTransformedItems(
     ApplicationConnection connection,
     HealthRecordAccessor accessor,
     HealthRecordSearcher searcher,
     string transform)
 {
     return HealthVaultPlatformItem.Current.GetTransformedItems(connection, accessor, searcher, transform);
 }
        /// <summary>
        /// Creates a new open query using the specified 
        /// <see cref="OfflineWebApplicationConnection"/>, definition, and personal
        /// identification number (PIN).
        /// </summary>
        /// 
        /// <param name="connection">
        /// A connection instance that creates the new open query.
        /// </param>
        /// 
        /// <param name="searcher">
        /// A <see cref="HealthRecordSearcher"/> instance that defines the open query.
        /// </param>
        /// 
        /// <param name="pinCode">
        /// The PIN that protects the query.  
        /// </param>
        /// 
        /// <returns>
        /// An <see cref="OpenQuery"/> instance that represents the newly created query.
        /// </returns>
        /// 
        /// <remarks>
        /// The creation of an open query makes public the data returned by that 
        /// query. However, the query URL must be known to retrieve the data.
        /// </remarks>
        /// 
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="connection"/> or <paramref name="searcher"/> 
        /// parameter is <b>null</b>.
        /// </exception>
        /// 
        /// <exception cref="ArgumentException">
        /// The <paramref name="searcher"/> parameter contains no valid search
        /// filters or the <paramref name="pinCode"/> parameter is <b>null</b> 
        /// or empty.
        /// </exception>
        /// 
        /// <exception cref="HealthServiceException">
        /// An error occurred when HealthVault processed the request.
        /// </exception>
        /// 
        public static OpenQuery NewQuery(
            OfflineWebApplicationConnection connection,
            HealthRecordSearcher searcher,
            string pinCode)
        {
            Validator.ThrowIfStringNullOrEmpty(pinCode, "pinCode");

            return NewQuery(
                connection,
                searcher,
                Int32.MaxValue,
                pinCode,
                String.Empty,
                null);
        }
 /// <summary>
 /// Creates a new open query using the specified 
 /// <see cref="OfflineWebApplicationConnection"/> and definition.
 /// </summary>
 /// 
 /// <param name="connection">
 /// An <see cref="OfflineWebApplicationConnection"/> instance that 
 /// creates the new open query.
 /// </param>
 /// 
 /// <param name="searcher">
 /// A <see cref="HealthRecordSearcher"/> instance that defines the open query.
 /// </param>
 /// 
 /// <returns>
 /// An <see cref="OpenQuery"/> instance that represents the newly created query.
 /// </returns>
 /// 
 /// <remarks>
 /// The creation of an open query makes public the data returned by that 
 /// query. However, the query URL must be known to retrieve the data.
 /// </remarks>
 /// 
 /// <exception cref="ArgumentNullException">
 /// The <paramref name="connection"/> parameter is <b>null</b>.
 /// </exception>
 /// 
 /// <exception cref="ArgumentNullException">
 /// The <paramref name="searcher"/> parameter is <b>null</b>.
 /// </exception>
 /// 
 /// <exception cref="ArgumentException">
 /// The <paramref name="searcher"/> parameter contains no valid
 /// search filters.
 /// </exception>
 /// 
 /// <exception cref="HealthServiceException">
 /// An error occurred when HealthVault processed the request.
 /// </exception>
 /// 
 public static OpenQuery NewQuery(
     OfflineWebApplicationConnection connection,
     HealthRecordSearcher searcher)
 {
     return NewQuery(
         connection,
         searcher,
         Int32.MaxValue,
         String.Empty,
         String.Empty,
         null);
 }
 /// <summary>
 /// Creates a new open query using the specified 
 /// <see cref="AuthenticatedConnection"/> and definition.
 /// </summary>
 /// 
 /// <param name="connection">
 /// An <see cref="AuthenticatedConnection"/> instance that creates the new open query.
 /// </param>
 /// 
 /// <param name="searcher">
 /// A <see cref="HealthRecordSearcher"/> instance that defines the open query.
 /// </param>
 /// 
 /// <returns>
 /// An <see cref="OpenQuery"/> instance that represents the newly created query.
 /// 
 /// </returns>
 /// 
 /// <remarks>
 /// The creation of an open query makes the data returned by that 
 /// query public. The only obscurity to the data is that the query
 /// URL must be known to retrieve it.
 /// </remarks>
 /// 
 /// <exception cref="ArgumentNullException">
 /// The <paramref name="connection"/> parameter is <b>null</b>.
 /// </exception>
 /// 
 /// <exception cref="ArgumentNullException">
 /// The <paramref name="searcher"/> parameter is <b>null</b>.
 /// </exception>
 /// 
 /// <exception cref="ArgumentException">
 /// The <paramref name="searcher"/> parameter contains no valid
 /// search filters.
 /// </exception>
 /// 
 /// <exception cref="HealthServiceException">
 /// An error occurred when HealthVault processed the request.
 /// </exception>
 /// 
 public static OpenQuery NewQuery(
     AuthenticatedConnection connection,
     HealthRecordSearcher searcher)
 {
     return NewQuery(
         connection,
         searcher,
         Int32.MaxValue,
         String.Empty,
         String.Empty,
         null);
 }
        /// <summary>
        /// Gets a single health record item from the associated record by 
        /// using the item identifier.
        /// </summary>
        /// 
        /// <param name="itemId">
        /// The unique identifier for the health record item.
        /// </param>
        /// 
        /// <param name="sections">
        /// The data sections of the health record item that should be retrieved.
        /// </param>
        /// 
        /// <returns>
        /// An instance of a <see cref="Microsoft.Health.HealthRecordItem"/> 
        /// representing the health record item with the specified identifier.
        /// </returns>
        /// 
        /// <remarks>
        /// This method accesses the HealthVault service across the network.
        /// <br/><br/>
        /// All filters are cleared and replaced with a single filter
        /// for the specified item.
        /// </remarks>
        /// 
        /// <exception cref="HealthServiceException">
        /// The server returned something other than a code of 
        /// HealthServiceStatusCode.OK, or the result count did not equal one (1).
        /// -or-
        /// <see cref="Microsoft.Health.HealthRecordSearcher.Filters"/> is empty
        /// or contains invalid filters.
        /// </exception>
        /// 
        public HealthRecordItem GetSingleItem(
            Guid itemId,
            HealthRecordItemSections sections)
        {
            // Create a new searcher to get the item.
            HealthRecordSearcher searcher = new HealthRecordSearcher(Record);

            HealthRecordFilter filter = new HealthRecordFilter();
            filter.ItemIds.Add(itemId);
            filter.View.Sections = sections;
            filter.CurrentVersionOnly = true;

            searcher.Filters.Add(filter);

            ReadOnlyCollection<HealthRecordItemCollection> resultSet =
                HealthVaultPlatform.GetMatchingItems(Record.Connection, Record, searcher);

            // Check in case HealthVault returned invalid data.
            if (resultSet.Count > 1)
            {
                HealthServiceResponseError error = new HealthServiceResponseError();
                error.Message =
                    ResourceRetriever.GetResourceString(
                        "GetSingleThingTooManyResults");

                HealthServiceException e =
                    HealthServiceExceptionHelper.GetHealthServiceException(
                        HealthServiceStatusCode.MoreThanOneThingReturned,
                        error);
                throw e;
            }

            HealthRecordItem result = null;
            if (resultSet.Count == 1)
            {
                HealthRecordItemCollection resultGroup = resultSet[0];

                if (resultGroup.Count > 1)
                {
                    HealthServiceResponseError error = new HealthServiceResponseError();
                    error.Message =
                        ResourceRetriever.GetResourceString(
                            "GetSingleThingTooManyResults");

                    HealthServiceException e =
                        HealthServiceExceptionHelper.GetHealthServiceException(
                            HealthServiceStatusCode.MoreThanOneThingReturned,
                            error);
                    throw e;
                }

                if (resultGroup.Count == 1)
                {
                    result = resultGroup[0];
                }
            }
            return result;
        }