using System.IO; using System.Xml; using System.Xml.Linq; using System.Xml.Xsl; namespace Neo.Afx.ComponentModel { public partial class Extensions { #region GetXElement /// /// Converts an to an . /// /// The node to be converted. /// An . public static XElement GetXElement(this XmlNode node) { var xDoc = new XDocument(); if(node != null) { using(var xmlWriter = xDoc.CreateWriter()) { node.WriteTo(xmlWriter); } } return xDoc.Root; } #endregion #region GetXmlNode /// /// Converts an to an . /// /// The node to be converted. /// An . public static XmlNode GetXmlNode(this XElement element) { var xmlDoc = new XmlDocument(); if(element != null) { using(var xmlReader = element.CreateReader()) { xmlDoc.Load(xmlReader); } } return xmlDoc; } #endregion #region TransForm /// /// Applies the given XSLT to the given XML. /// /// The XML to be transformed. /// The XSL to be applied. /// The result of the XSL transformation. public static XDocument Transform(this string xml, string xsl) { return Transform(XElement.Parse(xml), xsl); } /// /// Applies the given XSLT to the given XML. /// /// The XML to be transformed. /// The XSL to be applied. /// The result of the XSL transformation. public static XDocument Transform(this XElement xml, string xsl) { var result = new XDocument(); try { using(var xslReader = XmlReader.Create(new StringReader(xsl))) { var xslt = new XslCompiledTransform(); xslt.Load(xslReader); using(var xslWriter = result.CreateWriter()) { xslt.Transform(xml.CreateReader(), xslWriter); } } } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { // Ignore and return empty } return result; } #endregion } }