/// <summary>
        /// Create the info section for the cast call.
        /// </summary>
        /// <returns>The infor section.</returns>
        internal XElement CreateCastCallInfoSection()
        {
            XElement content = new XElement("content",
                                            new XElement("app-id", AppIdInstance.ToString()),
                                            new XElement("hmac", "HMACSHA256"),
                                            new XElement("signing-time", DateTime.UtcNow.ToString("O"))
                                            );

            XElement outer = new XElement("outer", content);

            XmlReader reader = outer.CreateReader();

            reader.MoveToContent();
            string s = reader.ReadInnerXml();

            s = CHBaseService.GetOuterXml(content);

            string hmac = MobilePlatform.ComputeSha256Hmac(Convert.FromBase64String(SharedSecret), s);

            XElement info = new XElement("info",
                                         new XElement("auth-info",
                                                      new XElement("app-id", AppIdInstance.ToString()),
                                                      new XElement("credential",
                                                                   new XElement("appserver2",
                                                                                new XElement("hmacSig", hmac,
                                                                                             new XAttribute("algName", "HMACSHA256")
                                                                                             ),
                                                                                content
                                                                                )
                                                                   )
                                                      )
                                         );

            return(info);
        }
        /// <summary>
        /// Loads the last-saved configuration from isolated storage.
        /// </summary>
        /// <param name="name">The filename to use.</param>
        public void LoadSettings(string name)
        {
            string settingsXml = MobilePlatform.ReadTextFromFile(name + ".xml");

            if (settingsXml != null)
            {
                XElement settings = XElement.Parse(settingsXml);

                string version = settings.Element("Version").Value;

                string applicationIdString = settings.Element("ApplicationId").Value;
                AppIdInstance             = new Guid(applicationIdString);
                AuthorizationSessionToken = settings.Element("AuthorizationSessionToken").Value;
                SharedSecret        = settings.Element("SharedSecret").Value;
                Country             = settings.Element("Country").Value;
                Language            = settings.Element("Language").Value;
                SessionSharedSecret = settings.Element("SessionSharedSecret").Value;

                // Create a temporary current record until we fetch the real one.
                XElement personIdNode = settings.Element("PersonId");
                if (settings.Element("PersonId") != null)
                {
                    Guid personId = new Guid(settings.Element("PersonId").Value);
                    Guid recordId = new Guid(settings.Element("RecordId").Value);

                    CurrentRecord = new CHBaseRecord(personId, recordId);
                }
            }
        }
        /// <summary>
        /// Saves the current configuration to isolated storage.
        /// </summary>
        /// <param name="name">The filename to use.</param>
        public void SaveSettings(string name)
        {
            XElement settings = new XElement("HealthVaultSettings",
                                             new XElement("Version", SettingsVersion),
                                             new XElement("ApplicationId", AppIdInstance.ToString()),
                                             new XElement("ApplicationCreationToken", ApplicationCreationToken),
                                             new XElement("AuthorizationSessionToken", AuthorizationSessionToken),
                                             new XElement("SharedSecret", SharedSecret),
                                             new XElement("Country", Country),
                                             new XElement("Language", Language),
                                             new XElement("SessionSharedSecret", SessionSharedSecret)
                                             );

            if (CurrentRecord != null)
            {
                settings.Add(new XElement("PersonId", CurrentRecord.PersonId.ToString()),
                             new XElement("RecordId", CurrentRecord.RecordId.ToString()));
            }

            MobilePlatform.SaveTextToFile(name + ".xml", settings.ToString());
        }
        /// <summary>
        /// Generate the XML for a request.
        /// </summary>
        /// <param name="clientRequest">The request.</param>
        /// <returns>The XML representation.</returns>
        internal string GenerateRequestXml(CHBaseRequest clientRequest)
        {
            XElement request = XElement.Parse(@"<wc-request:request xmlns:wc-request=""urn:com.microsoft.wc.request"" />");

            XElement header = new XElement("header");
            {
                header.Add(new XElement("method", clientRequest.MethodName));
                header.Add(new XElement("method-version", clientRequest.MethodVersion));

                if (CurrentRecord != null)
                {
                    header.Add(new XElement("record-id", CurrentRecord.RecordId.ToString()));
                }

                if (!String.IsNullOrEmpty(AuthorizationSessionToken))
                {
                    XElement authSession = new XElement("auth-session");
                    authSession.Add(new XElement("auth-token", AuthorizationSessionToken));

                    if (CurrentRecord != null)
                    {
                        authSession.Add(new XElement("offline-person-info",
                                                     new XElement("offline-person-id", CurrentRecord.PersonId.ToString())));
                    }

                    header.Add(authSession);
                }
                else
                {
                    if (AppIdInstance == Guid.Empty)
                    {
                        header.Add(new XElement("app-id", MasterAppId.ToString()));
                    }
                    else
                    {
                        header.Add(new XElement("app-id", AppIdInstance.ToString()));
                    }
                }

                header.Add(new XElement("language", Language));
                header.Add(new XElement("country", Country));
                header.Add(new XElement("msg-time", clientRequest.MessageTime.ToUniversalTime().ToString("O")));
                header.Add(new XElement("msg-ttl", "1800"));
                header.Add(new XElement("version", MobilePlatform.PlatformAbbreviationAndVersion));
            }

            XElement info = new XElement("info");

            if (clientRequest.InfoSection != null)
            {
                info = clientRequest.InfoSection;
            }

            if (clientRequest.MethodName != "CreateAuthenticatedSessionToken")
            {
                // if we have an info section, we need to compute the hash of that and put it in the header.
                if (clientRequest.InfoSection != null)
                {
                    string infoString = GetOuterXml(info);
                    header.Add(new XElement("info-hash",
                                            MobilePlatform.ComputeSha256HashAndWrap(infoString)));
                }

                if (!String.IsNullOrEmpty(SessionSharedSecret))
                {
                    byte[] sharedSecretKey = Convert.FromBase64String(SessionSharedSecret);
                    string headerXml       = GetOuterXml(header);

                    request.Add(new XElement("auth",
                                             MobilePlatform.ComputeSha256HmacAndWrap(sharedSecretKey, headerXml)));
                }
            }

            request.Add(header);
            request.Add(info);

            string requestString = GetOuterXml(request);

            return(requestString);
        }