Esempio n. 1
0
 public string Serialize(Content product)
 {
     using (var stream = new MemoryStream())
     {
         XamlConfigurationParser.SaveTo(stream, product);
         return(new StreamReader(stream).ReadToEnd());
     }
 }
Esempio n. 2
0
        protected virtual T GetOrAdd <T>(string key, Func <T> func)
        {
            var paths = new List <string>()
            {
                Path.Combine(DataDirectory, key)
            };

            if (ArchiveFiles)
            {
                paths.Insert(0, Path.Combine(DataDirectory, $"{key}.zip"));
            }
            foreach (var path in paths)
            {
                if (File.Exists(path))
                {
                    if (path.EndsWith("zip"))
                    {
                        using (var archive = ZipFile.OpenRead(path))
                        {
                            var entry = archive.Entries.FirstOrDefault();
                            using (var stream = entry.Open())
                            {
                                return((T)XamlConfigurationParser.LoadFrom(stream));
                            }
                        }
                    }
                    else
                    {
                        return((T)XamlConfigurationParser.LoadFrom(path));
                    }
                }
            }

            var data = func();

            if (data != null)
            {
                var path = paths[0];
                if (path.EndsWith("zip"))
                {
                    using (var memoryStream = File.Create(path))
                    {
                        using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create))
                        {
                            var entry = archive.CreateEntry(key);

                            using (var stream = entry.Open())
                            {
                                XamlConfigurationParser.SaveTo(stream, data);
                            }
                        }
                    }
                }
            }

            return(data);
        }
Esempio n. 3
0
 public Task Write(Stream stream, Article product)
 {
     return(Task.Run(() => XamlConfigurationParser.SaveTo(stream, product)));
 }