示例#1
0
        /// <summary>
        /// Create a single session file object from its raw data elements.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        public static SessionFileXml ToSessionFileXml(Guid id, int sequence, long version)
        {
            SessionFileXml newObject = new SessionFileXml();

            newObject.id       = id.ToString();
            newObject.sequence = sequence;
            newObject.version  = version;

            return(newObject);
        }
示例#2
0
        /// <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);
        }