Ejemplo n.º 1
0
        private void TransformXml()
        {
            XslCompiledTransform xslTransformer = new XslCompiledTransform();

            xslTransformer.Load(Server.MapPath(GetAttributeValue("XSLTFile")));

            Rock.Web.Cache.PageCache rootPage = null;

            Guid pageGuid = Guid.Empty;

            if (Guid.TryParse(GetAttributeValue(ROOT_PAGE), out pageGuid))
            {
                rootPage = Rock.Web.Cache.PageCache.Read(pageGuid);
            }

            if (rootPage == null)
            {
                rootPage = CurrentPage;
            }

            int levelsDeep = Convert.ToInt32(GetAttributeValue(NUM_LEVELS));

            Dictionary <string, string> pageParameters = null;

            if (GetAttributeValue("IncludeCurrentParameters").AsBoolean())
            {
                pageParameters = CurrentPageReference.Parameters;
            }

            NameValueCollection queryString = null;

            if (GetAttributeValue("IncludeCurrentQueryString").AsBoolean())
            {
                queryString = CurrentPageReference.QueryString;
            }

            XDocument pageXml = rootPage.MenuXml(levelsDeep, CurrentPerson, CurrentPage, pageParameters, queryString);

            StringBuilder sb = new StringBuilder();
            TextWriter    tw = new StringWriter(sb);

            xslTransformer.Transform(pageXml.CreateReader(), null, tw);

            phContent.Controls.Clear();
            phContent.Controls.Add(new LiteralControl(sb.ToString()));
        }
Ejemplo n.º 2
0
        private void TransformXml()
        {
            string outputString = string.Empty;

            // ensure xslt file exists
            string xsltFile = Server.MapPath(GetAttributeValue("XSLTFile"));

            if (System.IO.File.Exists(xsltFile))
            {
                // try compiling the XSLT
                try
                {
                    XslCompiledTransform xslTransformer = new XslCompiledTransform();
                    xslTransformer.Load(xsltFile);

                    Rock.Web.Cache.PageCache rootPage = null;

                    Guid pageGuid = Guid.Empty;
                    if (Guid.TryParse(GetAttributeValue(ROOT_PAGE), out pageGuid))
                    {
                        rootPage = Rock.Web.Cache.PageCache.Read(pageGuid);
                    }

                    if (rootPage == null)
                    {
                        rootPage = CurrentPage;
                    }

                    int levelsDeep = Convert.ToInt32(GetAttributeValue(NUM_LEVELS));

                    Dictionary <string, string> pageParameters = null;
                    if (GetAttributeValue("IncludeCurrentParameters").AsBoolean())
                    {
                        pageParameters = CurrentPageReference.Parameters;
                    }

                    NameValueCollection queryString = null;
                    if (GetAttributeValue("IncludeCurrentQueryString").AsBoolean())
                    {
                        queryString = CurrentPageReference.QueryString;
                    }

                    XDocument pageXml = rootPage.MenuXml(levelsDeep, CurrentPerson, CurrentPage, pageParameters, queryString);

                    // if debug the output the xml
                    string showDebugValue = GetAttributeValue("ShowDebug") ?? string.Empty;
                    bool   showDebug      = showDebugValue.Equals("true", StringComparison.OrdinalIgnoreCase);

                    if (showDebug || string.IsNullOrWhiteSpace(xsltFile))
                    {
                        outputString = "<pre><code>" + HttpUtility.HtmlEncode(pageXml.ToString()) + "</code></pre>";
                    }
                    else
                    {
                        // transform xml
                        StringBuilder sb = new StringBuilder();
                        TextWriter    tw = new StringWriter(sb);

                        xslTransformer.Transform(pageXml.CreateReader(), null, tw);

                        outputString = sb.ToString();
                    }
                }
                catch (Exception ex)
                {
                    // xslt compile error
                    string exMessage = "An excception occurred while compiling the XSLT template.";

                    if (ex.InnerException != null)
                    {
                        exMessage += "<br /><em>" + ex.InnerException.Message + "</em>";
                    }

                    outputString = "<div class='alert warning' style='margin: 24px auto 0 auto; max-width: 500px;' ><strong>XSLT Compile Error</strong><p>" + exMessage + "</p></div>";
                }
            }
            else
            {
                outputString = "<div class='alert warning' style='margin: 24px auto 0 auto; max-width: 500px;' ><strong>Warning!</strong><p>The XSLT file required to process the page list could not be found.</p><p><em>" + xsltFile + "</em></p>";
            }

            phContent.Controls.Clear();

            phContent.Controls.Add(new LiteralControl(outputString));
        }