8.29.2009

XML Web Part

It’s not superior in any way to the SharePoint web part, but I don’t have the source for the SharePoint web part just laying around either.  The code uses the XslCompiledTransform class to transform the xml provided.

 

 

using System.Web.UI.WebControls.WebParts;

using System.Xml.Xsl;

using System.Xml;

using System.IO;

using System;

using System.ComponentModel;

using System.Diagnostics;

 

namespace IdahoWebParts

{

    public class XMLWebPart : WebPart

    {

 

 

        [Personalizable, Browsable(true),WebBrowsable(true),Description("Url of Source XML Document")]

        public string SourceURL

        {

            get

            {

                return (string)ViewState["XmlURL"];

            }

 

            set

            {

                ViewState["XmlURL"] = value;

            }

        }

 

        [Personalizable, Browsable(true),WebBrowsable(true),Description("XML to transform")]

        public string Document

        {

            get

            {

                return (string)ViewState["XmlDocument"];

            }

 

            set

            {

                ViewState["XmlDocument"] = value;

            }

        }

 

        [Personalizable, Browsable(true),WebBrowsable(true),Description("URL of StyleSheet")]

        public string StyleSheetURL

        {

            get

            {

                return (string)ViewState["XsltURL"];

            }

 

            set

            {

                ViewState["XsltURL"] = value;

            }

        }

 

        [Personalizable, Browsable(true),WebBrowsable(true),Description("XSLT used to transform XML")]

        public string StyleSheet

        {

            get

            {

                return (string)ViewState["XsltDocument"];

            }

 

            set

            {

                ViewState["XsltDocument"] = value;

            }

        }

 

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)

        {

            try

            {

                // get the style sheet

                string transformDocument = (!string.IsNullOrEmpty(StyleSheetURL)) ? Utility.GetWebTextFile(Page.ResolveUrl(StyleSheetURL)) : StyleSheet;

 

                // get the xml

                string xmlDoc = (!string.IsNullOrEmpty(SourceURL)) ? Utility.GetWebTextFile(Page.ResolveUrl(SourceURL)) : Document;

 

 

                writer.Write(string.Format("<div id={0}>", UniqueID));

                // if we have a style sheet use it to transform the xml

                if (!string.IsNullOrEmpty(transformDocument) && !string.IsNullOrEmpty(xmlDoc))

                {

                    XslCompiledTransform transform = new XslCompiledTransform();

                    transform.Load(XmlReader.Create(new StringReader(transformDocument)));

                    transform.Transform(XmlReader.Create(new StringReader(xmlDoc)), XmlWriter.Create(writer));

                }

 

                else if (!string.IsNullOrEmpty(xmlDoc))

                {

                    writer.Write(xmlDoc);

                }

 

                writer.Write("</div>");

            }

 

            catch (System.Xml.XmlException)

            {

 

                writer.Write("XML error");

            }

 

            catch (Exception exception)

            {

                writer.Write("Error");

                Debug.WriteLine(exception.ToString());

            }

        }

    }

}

No comments: