Ejemplo n.º 1
0
        public string Serialize <T>(T data)
        {
            IXmlNamespaceProvider namespaceProvider = data as IXmlNamespaceProvider;
            IEnumerable <string>  namespaces        = namespaceProvider != null?namespaceProvider.GetNamespaces() : Enumerable.Empty <string>();

            var xmlSerializerNamespaces = _xmlNamespaceBuilder.Create(namespaces);


            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

            using (MemoryStream memoryStream = new MemoryStream())
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
                {
                    Encoding          = Encoding.UTF8,
                    NamespaceHandling = NamespaceHandling.OmitDuplicates
                };

                using (XmlWriter writer = XmlWriter.Create(memoryStream, xmlWriterSettings))
                {
                    xmlSerializer.Serialize(writer, data, xmlSerializerNamespaces);
                    writer.Flush();
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    return(new StreamReader(memoryStream).ReadToEnd());
                }
            }
        }
Ejemplo n.º 2
0
        private void SerializeToStream <T>(T data, Func <XmlWriterSettings, XmlWriter> createXmlWriter)
        {
            IXmlNamespaceProvider namespaceProvider = data as IXmlNamespaceProvider;
            IEnumerable <string>  namespaces        = namespaceProvider != null?namespaceProvider.GetNamespaces() : Enumerable.Empty <string>();

            XmlSerializerNamespaces xmlSerializerNamespaces = xmlNamespaceBuilder.Create(namespaces);

            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
            {
                Encoding          = Encoding.UTF8,
                NamespaceHandling = NamespaceHandling.OmitDuplicates
            };

            using (XmlWriter writer = createXmlWriter(xmlWriterSettings))
            {
                if (data is IHasStyleSheets)
                {
                    xmlProcessingInstructionHandler.AddStyleSheets(writer, data as IHasStyleSheets);
                }

                xmlSerializer.Serialize(writer, data, xmlSerializerNamespaces);
            }
        }
Ejemplo n.º 3
0
        public void SerializeToStream <T>(T data, Stream stream)
        {
            IXmlNamespaceProvider namespaceProvider = data as IXmlNamespaceProvider;
            IEnumerable <string>  namespaces        = namespaceProvider != null?namespaceProvider.GetNamespaces() : Enumerable.Empty <string>();

            XmlSerializerNamespaces xmlSerializerNamespaces = _xmlNamespaceBuilder.Create(namespaces);

            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
            {
                Encoding          = Encoding.UTF8,
                NamespaceHandling = NamespaceHandling.OmitDuplicates
            };

            using (XmlWriter writer = XmlWriter.Create(stream, xmlWriterSettings))
            {
                xmlSerializer.Serialize(writer, data, xmlSerializerNamespaces);
            }
        }