Ejemplo n.º 1
0
        /// <summary>
        /// Serializes the <paramref name="list"/> <see cref="PlayList"/> to the <paramref name="sw"/> <see cref="StreamWriter"/>
        /// </summary>
        /// <param name="list">The list to synchronize.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"/>
        public static void Serialize(StreamWriter sw, PlayList list)
        {
            StreamWriter sww = new StreamWriter(sw.BaseStream, Encoding.Default);
            XmlWriter    xw  = XmlWriter.Create(sww, new XmlWriterSettings()
            {
                Indent = true, OmitXmlDeclaration = true, NewLineOnAttributes = true
            });

            try
            {
                xmlSerializer.Serialize(xw, list, xmlsns);
            }
            catch (Exception e)
            {
                xw.Close();
                sw.Close();
                throw e;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserialize multiple XML files containing playlist data in list order <b>asynchronously</b>.
        /// <para>Returns <see cref="null"/> if all file isn't valid.</para>
        /// </summary>
        /// <param name="paths">The paths of the playlist files.</param>
        /// <param name="ignoreError">If set to true stops exceptions and returns null on error.</param>
        /// <param name="progress">Reports the percentage of completed lists.</param>
        /// <returns>A new <see cref="PlayList"/></returns>
        /// <exception cref="InvalidOperationException"/>
        public async static Task <PlayList> DeserializeAsync(string[] paths, IProgress <int> progress = null, bool ignoreError = false)
        {
            Exception exc = null;
            int       val = 0;
            List <Task <PlayList> > tasks = new List <Task <PlayList> >();

            foreach (string path in paths)
            {
                Task <PlayList> t = Task.Run(() =>
                {
                    try
                    {
                        var res = Deserialize(path, ignoreError);
                        progress.Report((++val * 100) / paths.Count());
                        return(res);
                    }
                    catch (Exception ex)
                    {
                        if (exc == null)
                        {
                            exc = ex;
                        }
                        return(new PlayList());
                    }
                });

                tasks.Add(t);
            }
            var results = new PlayList(await Task.WhenAll(tasks));

            if (exc != null)
            {
                throw exc;
            }
            return(results);
        }