Пример #1
0
        public static string SerializeToXml(this object serializableObject)
        {
            if (serializableObject == null)
            {
                throw new ArgumentNullException(nameof(serializableObject));
            }

            string output;
            var    serializer = new XmlSerializer(serializableObject.GetType());
            var    settings   = new XmlWriterSettings
            {
                Encoding           = Encoding.UTF8,
                Indent             = true,
                OmitXmlDeclaration = true,
                NewLineHandling    = NewLineHandling.None
            };
            var ns = new XmlSerializerNamespaces();

            ns.Add(string.Empty, string.Empty);
            using (var stringWriter = new CustomEncodingStringWriter(Encoding.UTF8))
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
                {
                    serializer.Serialize(xmlWriter, serializableObject, ns);
                }

                output = stringWriter.ToString();
            }
            return(output);
        }
Пример #2
0
        public static string PrettyXml(string xml)
        {
            if (string.IsNullOrWhiteSpace(xml))
            {
                throw new ArgumentNullException(nameof(xml));
            }

            string output;
            var    xmlDoc = XDocument.Parse(xml);

            using (var stringWriter = new CustomEncodingStringWriter(Encoding.UTF8))
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings
                {
                    Encoding = Encoding.UTF8,
                    Indent = true,
                    OmitXmlDeclaration = true,
                    NewLineHandling = NewLineHandling.None
                }))
                {
                    xmlDoc.Save(xmlWriter);
                }
                output = stringWriter.ToString();
            }
            return(output);
        }
Пример #3
0
        public static string PrettyXml(string xml)
        {
            if (string.IsNullOrWhiteSpace(xml))
            {
                throw new ArgumentNullException(nameof(xml));
            }
            var xDoc = XDocument.Parse(xml,
                                       LoadOptions.SetBaseUri | LoadOptions.SetLineInfo | LoadOptions.PreserveWhitespace);
            string output;

            using (var stringWriter = new CustomEncodingStringWriter(Encoding.UTF8))
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings
                {
                    Encoding = Encoding.UTF8,
                    Indent = true,
                    IndentChars = "\t",
                    NewLineChars = "\r\n",
                    NewLineHandling = NewLineHandling.Replace,
                    ConformanceLevel = ConformanceLevel.Document
                }))
                {
                    xDoc.Save(xmlWriter);
                }
                output = stringWriter.ToString();
            }
            return(output);
        }