Ejemplo n.º 1
0
        /// <summary>
        /// Repairs an html fragment (makes it Xhtml) and executes a transformation on it.
        /// </summary>
        /// <param name="html">The html to repair</param>
        /// <param name="xsltPath">The path to the XSLT to use for transformation</param>
        /// <param name="xsltParameters"></param>
        /// <param name="errorSummary">out value - warnings generated while repairing the html</param>
        /// <returns></returns>
        public static XDocument RepairXhtmlAndTransform(string html, string xsltPath, Dictionary <string, string> xsltParameters, out string errorSummary)
        {
            TidyHtmlResult tidyHtmlResult = MarkupTransformationServices.TidyHtml(html);

            errorSummary = tidyHtmlResult.ErrorSummary;
            XNode     tidiedXhtml    = tidyHtmlResult.Output;
            XDocument outputDocument = new XDocument();

            XslCompiledTransform xslt = XsltServices.GetCompiledXsltTransform(xsltPath);

            using (XmlWriter writer = outputDocument.CreateXhtmlWriter())
            {
                using (XmlReader reader = tidiedXhtml.CreateReader())
                {
                    if (xsltParameters != null && xsltParameters.Count > 0)
                    {
                        XsltArgumentList xsltArgumentList = new XsltArgumentList();
                        foreach (var xsltParameter in xsltParameters)
                        {
                            xsltArgumentList.AddParam(xsltParameter.Key, "", xsltParameter.Value);
                        }
                        xslt.Transform(reader, xsltArgumentList, writer);
                    }
                    else
                    {
                        xslt.Transform(reader, writer);
                    }
                }
            }

            return(outputDocument);
        }