Пример #1
0
        private static string SerializeXML(Design design)
        {
            MemoryStream      stream   = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Encoding    = Encoding.UTF8;
            settings.Indent      = true;
            settings.IndentChars = " ";
            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                string ns = "http://www.demoninspect.com/schemas/report-design";
                System.Xml.Serialization.XmlSerializer serializer =
                    new System.Xml.Serialization.XmlSerializer(design.GetType(), ns);
                serializer.Serialize(writer, design);
            }
            stream.Capacity = (int)stream.Length;
            return(Encoding.UTF8.GetString(stream.GetBuffer()));
        }
Пример #2
0
        private static Stream Serialize(Design design, string mimeType)
        {
            switch (mimeType)
            {
            case Demon.Core.MimeType.XML:
            {
                MemoryStream      stream   = new MemoryStream();
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding    = Encoding.UTF8;
                settings.Indent      = true;
                settings.IndentChars = " ";
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    string ns = "http://www.demoninspect.com/schemas/report-design";
                    System.Xml.Serialization.XmlSerializer serializer =
                        new System.Xml.Serialization.XmlSerializer(design.GetType(), ns);
                    serializer.Serialize(writer, design);
                }
                stream.Capacity = (int)stream.Length;
                stream.Seek(0, SeekOrigin.Begin);
                return(stream);
            }

            case Demon.Core.MimeType.JSON:
            {
                MemoryStream stream = new MemoryStream();
                using (StreamWriter sw = new StreamWriter(stream))
                {
                    JsonTextWriter jw = new JsonTextWriter(sw);
                    jw.Formatting = Newtonsoft.Json.Formatting.Indented;

                    design.WriteJson(jw);

                    jw.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    return(stream);
                }
            }

            default:
                throw new ArgumentException($"Cannot serialize design to MIME type {mimeType}.");
            }
        }