public static void SaveSnippetsToSingleFile( IEnumerable <Snippet> snippets, string filePath, bool onlyIfChanged = true) { if (snippets == null) { throw new ArgumentNullException(nameof(snippets)); } SaveSettings settings = CreateSaveSettings(); string content = SnippetSerializer.CreateXml(snippets, settings); if (!ShouldSave(filePath, content, Encoding.UTF8, onlyIfChanged)) { return; } using (var fileStream = new FileStream(filePath, FileMode.Create)) { Console.WriteLine($"saving file {filePath}"); SnippetSerializer.Serialize(fileStream, snippets, settings); } }
private static bool ShouldSave(Snippet snippet, string filePath, SaveSettings settings, bool onlyIfChanged) { if (!onlyIfChanged) { return(true); } if (!File.Exists(filePath)) { return(true); } string s1 = File.ReadAllText(filePath, Encoding.UTF8); string s2 = SnippetSerializer.CreateXml(snippet, settings); return(!string.Equals(s1, s2, StringComparison.Ordinal)); }
public void Snippet_Deserialize() { const string filePath = @"..\..\..\Snippet.snippet"; Snippet snippet = null; using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) snippet = SnippetSerializer.Deserialize(stream).FirstOrDefault(); var settings = new SaveSettings() { IndentChars = " ", OmitCodeSnippetsElement = true, OmitXmlDeclaration = true, Comment = " comment " }; string oldValue = File.ReadAllText(filePath, Encoding.UTF8); string newValue = SnippetSerializer.CreateXml(snippet, settings); Assert.Equal(oldValue, newValue); }