Ejemplo n.º 1
0
        /// <summary>
        /// Set the encoding of this XML document to the encoding given, if not set (null) default encoding is UTF8.
        /// Returns this XML document converted to it's string representation.
        /// </summary>

        public static string AsString(this XDocument xmlDoc, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            using (var sw = new ExtentedStringWriter(new StringBuilder(), encoding))
            {
                using (var tx = new XmlTextWriter(sw))
                {
                    xmlDoc.WriteTo(tx);
                    return(sw.ToString());
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method use XML Transformation to get only relevant information from a original XML element,
        /// we can find more information from XML Transformation in http://www.w3schools.com/xsl/xsl_examples.asp
        /// </summary>
        /// <param name="originalContent">Original XML Content to transform</param>
        /// <param name="xsltFullPath">Full Path of the Transformation file</param>
        /// <param name="argList">Optional parameter list</param>
        /// <returns>A new XElement with the transformed XML</returns>

        public static XElement RunToXElementTransform(this XElement originalContent, string xsltFullPath, XsltArgumentList argList = null)
        {
            XElement output;

            using (var sri = new StringReader(originalContent.ToString()))
            {
                using (var xri = XmlReader.Create(sri))
                {
                    var xslt         = new XslCompiledTransform();
                    var xsltSettings = new XsltSettings {
                        EnableScript = true
                    };

                    FileInfo fileInfo = null;

                    try
                    {
                        fileInfo = new FileInfo(xsltFullPath);
                    }
                    catch (Exception ex)
                    {
                        throw new FileNotFoundException("XSLT file is not found", ex);
                    }

                    // Load the Transformation Template File.
                    xslt.Load(fileInfo.FullName, xsltSettings, null);

                    // Use extended string writer to set encoding.
                    using (var sw = new ExtentedStringWriter(new StringBuilder(), Encoding.UTF8))
                        using (XmlWriter xwo = XmlWriter.Create(sw, xslt.OutputSettings))
                        {
                            if (argList != null)
                            {
                                xslt.Transform(xri, argList, xwo);
                            }
                            else
                            {
                                xslt.Transform(xri, xwo);
                            }

                            output = XElement.Parse(sw.ToString());
                        }
                }
            }

            return(output);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method use XML Transformation to get only relevant information from a original XML element,
        /// we can find more information from XML Transformation in http://www.w3schools.com/xsl/xsl_examples.asp
        /// </summary>
        /// <param name="originalContent">Original XML Content to transform</param>
        /// <param name="xsltString">The transformation style sheet to use</param>
        /// <param name="argList">Optional parameter list</param>
        /// <returns>A new XElement with the transformed XML</returns>

        public static XElement ParseXsltTransform(this XElement originalContent, string xsltString, XsltArgumentList argList = null)
        {
            XElement output;

            if (string.IsNullOrEmpty(xsltString))
            {
                throw new ArgumentException("XSLT input string is null or empty", "xsltString");
            }

            using (var sri = new StringReader(originalContent.ToString()))
                using (var xri = XmlReader.Create(sri))
                {
                    var xslt         = new XslCompiledTransform();
                    var xsltSettings = new XsltSettings {
                        EnableScript = true
                    };

                    using (var xmlReader = new StringReader(xsltString))
                        using (var reader = XmlReader.Create(xmlReader))
                        {
                            // Load the Transformation Template File.
                            xslt.Load(reader, xsltSettings, null);

                            // Use extended string writer to set encoding.
                            using (var sw = new ExtentedStringWriter(new StringBuilder(), Encoding.UTF8))
                                using (var xwo = XmlWriter.Create(sw, xslt.OutputSettings))
                                {
                                    if (argList != null)
                                    {
                                        xslt.Transform(xri, argList, xwo);
                                    }
                                    else
                                    {
                                        xslt.Transform(xri, xwo);
                                    }

                                    output = XElement.Parse(sw.ToString());
                                }
                        }
                }

            return(output);
        }