/// <summary> /// Implemented by inheritors to perform the request on the provided web client. /// </summary> /// <param name="connection"></param> protected override async Task OnProcessRequest(IWebChannelConnection connection) { byte[] requestedSessionsRawData = await connection.DownloadData(string.Format("/Hub/Hosts/{0}/RequestedSessions.xml", ClientId)).ConfigureAwait(false); //even though it's a session list we can't actually deserialize it directly - because we cant use XmlSerializer //since the types will not necessarily be public. RequestedSessions = DataConverter.ByteArrayToSessionsListXml(requestedSessionsRawData); }
/// <summary> /// Convert a byte array to sessions list XML object without relying on XML Serializer /// </summary> /// <param name="rawData"></param> /// <returns></returns> public static SessionsListXml ByteArrayToSessionsListXml(byte[] rawData) { SessionsListXml sessionsListXml = new SessionsListXml(); using (MemoryStream inputStream = new MemoryStream(rawData)) { using (TextReader textReader = new StreamReader(inputStream, Encoding.UTF8)) { using (XmlReader xmlReader = XmlReader.Create(textReader)) { //to load up the first element. if (xmlReader.ReadToFollowing("SessionsListXml") == false) { //it isn't a sessions list.. throw new InvalidDataException("The provided XML data is not a sessions list"); } string sessionsVersionRaw = xmlReader.GetAttribute("version"); if (string.IsNullOrEmpty(sessionsVersionRaw) == false) { sessionsListXml.version = long.Parse(sessionsVersionRaw); } if (xmlReader.ReadToFollowing("sessions")) { //this is a repeating section so we have to be ready for that... List <SessionXml> sessions = new List <SessionXml>(); bool moreSessions = true; while (moreSessions) { xmlReader.ReadStartElement(); if (xmlReader.LocalName.Equals("session")) { string guidRaw = xmlReader.GetAttribute("id"); string versionRaw = xmlReader.GetAttribute("version"); string deletedRaw = xmlReader.GetAttribute("deleted"); #if DEBUG if ((string.IsNullOrEmpty(guidRaw)) && (Debugger.IsAttached)) { Debugger.Break(); } #endif //now convert to a SessionXml object and add to the item. SessionXml newSession = new SessionXml(); newSession.id = guidRaw; newSession.version = long.Parse(versionRaw); newSession.deleted = bool.Parse(deletedRaw); sessions.Add(newSession); //and if this isn't an empty session element, we need to end it. if (xmlReader.IsEmptyElement == false) { xmlReader.ReadEndElement(); } } else { moreSessions = false; } } sessionsListXml.sessions = sessions.ToArray(); } } } } return(sessionsListXml); }