public async Task <rss> GetStreamList(String queryUrl, int offset, CancellationToken cancellationToken)
        {
            using (var response = await _httpClient.GetResponse(new HttpRequestOptions
            {
                CancellationToken = cancellationToken,
                Url = queryUrl,
                EnableDefaultUserAgent = true,
                EnableHttpCompression = false,
                BufferContent = false,
                EnableKeepAlive = false
            }).ConfigureAwait(false))
            {
                using (var xml = response.Content)
                {
                    _logger.Info("Reading TWiT response with StreamReader");

                    using (var reader = new StreamReader(xml))
                    {
                        var str = reader.ReadToEnd();

                        _logger.Info("Deserializing TwiT response");

                        rss result = _xmlSerializer.DeserializeFromBytes(typeof(rss), Encoding.UTF8.GetBytes(str)) as rss;
                        _logger.Info("Deserialized TwiT response");
                        return(result);
                    }
                }
            }
        }
        /// <summary>
        /// Reads an xml configuration file from the file system
        /// It will immediately re-serialize and save if new serialization data is available due to property changes.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="xmlSerializer">The XML serializer.</param>
        /// <returns>System.Object.</returns>
        public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
        {
            object configuration;

            byte[] buffer = null;

            // Use try/catch to avoid the extra file system lookup using File.Exists
            try
            {
                buffer = File.ReadAllBytes(path);

                configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
            }
            catch (Exception)
            {
                configuration = Activator.CreateInstance(type);
            }

            using var stream = new MemoryStream();
            xmlSerializer.SerializeToStream(configuration, stream);

            // Take the object we just got and serialize it back to bytes
            var newBytes = stream.ToArray();

            // If the file didn't exist before, or if something has changed, re-save
            if (buffer == null || !buffer.SequenceEqual(newBytes))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));

                // Save it after load in case we got new items
                File.WriteAllBytes(path, newBytes);
            }

            return(configuration);
        }
Exemple #3
0
        private List <T> GetItemsFromFile(string path)
        {
            var jsonFile = path + ".json";

            try
            {
                return(_jsonSerializer.DeserializeFromFile <List <T> >(jsonFile));
            }
            catch (FileNotFoundException)
            {
            }
            catch (DirectoryNotFoundException ex)
            {
            }
            catch (IOException ex)
            {
                Logger.ErrorException("Error deserializing {0}", ex, jsonFile);
                throw;
            }
            catch (Exception ex)
            {
                Logger.ErrorException("Error deserializing {0}", ex, jsonFile);
                return(new List <T>());
            }

            var xmlFile = path + ".xml";

            try
            {
                var xml   = ModifyInputXml(File.ReadAllText(xmlFile));
                var bytes = Encoding.UTF8.GetBytes(xml);

                return((List <T>)_serializer.DeserializeFromBytes(typeof(List <T>), bytes));
            }
            catch (FileNotFoundException)
            {
                return(new List <T>());
            }
            catch (DirectoryNotFoundException ex)
            {
                return(new List <T>());
            }
            catch (IOException ex)
            {
                Logger.ErrorException("Error deserializing {0}", ex, xmlFile);
                throw;
            }
            catch (Exception ex)
            {
                Logger.ErrorException("Error deserializing {0}", ex, xmlFile);
                return(new List <T>());
            }
        }
        /// <summary>
        /// Reads an xml configuration file from the file system
        /// It will immediately re-serialize and save if new serialization data is available due to property changes.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="xmlSerializer">The XML serializer.</param>
        /// <returns>System.Object.</returns>
        public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
        {
            object configuration;

            byte[]? buffer = null;

            // Use try/catch to avoid the extra file system lookup using File.Exists
            try
            {
                buffer = File.ReadAllBytes(path);

                configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
            }
            catch (Exception)
            {
                // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
                configuration = Activator.CreateInstance(type) !;
            }

            using var stream = new MemoryStream(buffer?.Length ?? 0);
            xmlSerializer.SerializeToStream(configuration, stream);

            // Take the object we just got and serialize it back to bytes
            byte[] newBytes    = stream.GetBuffer();
            int    newBytesLen = (int)stream.Length;

            // If the file didn't exist before, or if something has changed, re-save
            if (buffer == null || !newBytes.AsSpan(0, newBytesLen).SequenceEqual(buffer))
            {
                var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));

                Directory.CreateDirectory(directory);
                // Save it after load in case we got new items
                // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    fs.Write(newBytes, 0, newBytesLen);
                }
            }

            return(configuration);
        }
Exemple #5
0
        public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
        {
            object configuration;

            byte[]? buffer = null;

            // Use try/catch to avoid the extra file system lookup using File.Exists
            try
            {
                buffer = File.ReadAllBytes(path);

                configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
            }
            catch (Exception)
            {
                configuration = Activator.CreateInstance(type);
            }

            using var stream = new MemoryStream(buffer?.Length ?? 0);
            xmlSerializer.SerializeToStream(configuration, stream);

            // Take the object we just got and serialize it back to bytes
            byte[] newBytes    = stream.GetBuffer();
            int    newBytesLen = (int)stream.Length;

            // If the file didn't exist before, or if something has changed, re-save
            if (buffer == null || !newBytes.AsSpan(0, newBytesLen).SequenceEqual(buffer))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));

                // Save it after load in case we got new items
                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    fs.Write(newBytes, 0, newBytesLen);
                }
            }

            return(configuration);
        }
        /// <summary>
        /// Reads an xml configuration file from the file system
        /// It will immediately re-serialize and save if new serialization data is available due to property changes
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="xmlSerializer">The XML serializer.</param>
        /// <returns>System.Object.</returns>
        public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
        {
            object configuration;

            byte[] buffer = null;

            // Use try/catch to avoid the extra file system lookup using File.Exists
            try
            {
				buffer = File.ReadAllBytes(path);

                configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
            }
            catch (Exception)
            {
                configuration = Activator.CreateInstance(type);
            }

            using (var stream = new MemoryStream())
            {
                xmlSerializer.SerializeToStream(configuration, stream);

                // Take the object we just got and serialize it back to bytes
                var newBytes = stream.ToArray();

                // If the file didn't exist before, or if something has changed, re-save
                if (buffer == null || !buffer.SequenceEqual(newBytes))
                {
					Directory.CreateDirectory(Path.GetDirectoryName(path));

                    // Save it after load in case we got new items
					File.WriteAllBytes(path, newBytes);
                }

                return configuration;
            }
        }