Exemple #1
0
        /// <summary>
        /// Returns enumerable collection of <see cref="Snippet"/> contained by a specified <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> that contains snippets to deserialize.</param>
        /// <returns>Enumerable collection of snippets being deserialized.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> is <c>null</c>.</exception>
        public static IEnumerable <Snippet> Deserialize(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            return(Deserialize());

            IEnumerable <Snippet> Deserialize()
            {
                using (XmlReader xmlReader = XmlReader.Create(stream, XmlReaderSettings))
                {
                    while (xmlReader.Read() && xmlReader.NodeType != XmlNodeType.Element)
                    {
                    }

                    switch (xmlReader.Name)
                    {
                    case "CodeSnippet":
                    {
                        CodeSnippetElement element = Deserialize <CodeSnippetElement>(xmlReader, CodeSnippetElementXmlSerializer);

                        yield return(SnippetMapper.MapFromElement(element));

                        break;
                    }

                    case "CodeSnippets":
                    {
                        CodeSnippetsElement element = Deserialize <CodeSnippetsElement>(xmlReader, CodeSnippetsElementXmlSerializer);

                        if (element.Snippets == null)
                        {
                            break;
                        }

                        for (int i = 0; i < element.Snippets.Length; i++)
                        {
                            yield return(SnippetMapper.MapFromElement(element.Snippets[i]));
                        }

                        break;
                    }
                    }
                }
            }
        }
        /// <summary>
        /// Maps each element of <see cref="CodeSnippetElement"/> sequence to the newly created <see cref="CodeSnippetElement"/>, optionally modifying serialization process.
        /// </summary>
        /// <param name="snippets">An enumerable collection of code snippets to be serialized.</param>
        /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param>
        /// <returns>An enumerable collection of <see cref="CodeSnippetElement"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="snippets"/> is <c>null</c>.</exception>
        public static IEnumerable <CodeSnippetElement> MapToElement(IEnumerable <Snippet> snippets, SaveSettings settings)
        {
            if (snippets == null)
            {
                throw new ArgumentNullException(nameof(snippets));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            return(MapToElement());

            IEnumerable <CodeSnippetElement> MapToElement()
            {
                foreach (Snippet snippet in snippets)
                {
                    yield return(SnippetMapper.MapToElement(snippet, settings));
                }
            }
        }
Exemple #3
0
 private static void Serialize(XmlWriter xmlWriter, IEnumerable <Snippet> snippets, SaveSettings settings)
 {
     Serialize(xmlWriter, SnippetMapper.MapToElement(snippets, settings).ToArray(), settings);
 }
Exemple #4
0
 private static void Serialize(XmlWriter xmlWriter, Snippet snippet, SaveSettings settings)
 {
     Serialize(xmlWriter, new CodeSnippetElement[] { SnippetMapper.MapToElement(snippet, settings) }, settings);
 }