/// <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) { string url; if (ClientId.HasValue) { url = string.Format("/Hub/Hosts/{0}/{1}", ClientId, GenerateResourceUri()); } else { url = string.Format("/Hub/{0}", GenerateResourceUri()); } byte[] sessionFilesListRawData = await connection.DownloadData(url).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. Files = DataConverter.ByteArrayToSessionFilesListXml(sessionFilesListRawData); }
/// <summary> /// Convert a raw byte array to a session files list without using .NET XML Serialization /// </summary> /// <param name="rawData"></param> /// <returns></returns> public static SessionFilesListXml ByteArrayToSessionFilesListXml(byte[] rawData) { SessionFilesListXml sessionsFilesListXml = new SessionFilesListXml(); 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("SessionFilesListXml") == false) { //it isn't a sessions list.. throw new InvalidDataException("The provided XML data is not a session files list"); } string sessionsVersionRaw = xmlReader.GetAttribute("version"); if (string.IsNullOrEmpty(sessionsVersionRaw) == false) { sessionsFilesListXml.version = long.Parse(sessionsVersionRaw); } if (xmlReader.ReadToFollowing("files")) { //this is a repeating section so we have to be ready for that... List <SessionFileXml> files = new List <SessionFileXml>(); bool moreFiles = true; while (moreFiles) { xmlReader.ReadStartElement(); if (xmlReader.LocalName.Equals("file")) { string guidRaw = xmlReader.GetAttribute("id"); string sequenceRaw = xmlReader.GetAttribute("sequence"); string versionRaw = xmlReader.GetAttribute("version"); #if DEBUG if (string.IsNullOrEmpty(guidRaw) && Debugger.IsAttached) { Debugger.Break(); } #endif //now convert to a SessionFileXml object and add to the item. SessionFileXml newFile = new SessionFileXml(); newFile.id = guidRaw; newFile.sequence = int.Parse(sequenceRaw); newFile.version = long.Parse(versionRaw); files.Add(newFile); //and if this isn't an empty session element, we need to end it. if (xmlReader.IsEmptyElement == false) { xmlReader.ReadEndElement(); } } else { moreFiles = false; } } sessionsFilesListXml.files = files.ToArray(); } } } } return(sessionsFilesListXml); }