Exemple #1
0
        /// <summary>
        /// Return formatted XML string from current XDocument
        /// </summary>
        /// <param name="settings">Optional Settings</param>
        public static string ToFormattedXmlString(this XDocument value, XmlWriterSettings settings = null)
        {
            if (settings == null)
            {
                settings = GetDefaultSettings();
            }

            using (var sw = new StringWriterEncoded(settings.Encoding ?? Encoding.UTF8))
                using (var xw = XmlWriter.Create(sw, settings))
                {
                    value.WriteTo(xw);
                    xw.Flush();
                    return(sw.ToString());
                }
        }
Exemple #2
0
        public static bool Transform(string xmlToRender, string xslTransformation, out string xmlRendered, out Exception exception)
        {
            //***** Initialize;
            var result = false;

            xmlRendered = null;
            exception   = null;

            //*****
            try
            {
                //***** Load the XML document using the specified file name;
                XPathDocument xPathDocument;
                using (var stringReader = new StringReader(xmlToRender))
                    using (var xmlReader = XmlReader.Create(stringReader))
                        xPathDocument = new XPathDocument(xmlReader);

                //***** Load the XSL transformation using the specified file name;
                var xslCompiledTransform = new XslCompiledTransform();
                using (var stringReader = new StringReader(xslTransformation))
                    using (var xmlReader = XmlReader.Create(stringReader))
                        xslCompiledTransform.Load(xmlReader);

                //***** Create output file stream using Utf-8 encoding;
                var xml = new StringBuilder();
                using (var stringWriter = new StringWriterEncoded(xml, Encoding.GetEncoding("utf-8")))
                    using (var xmlWriter = XmlWriter.Create(stringWriter))
                        xslCompiledTransform.Transform(xPathDocument, null, xmlWriter);

                //*****
                xmlRendered = xml.ToString();

                //*****
                result = true;
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            //*****
            return(result);
        }