/// <summary> /// Perform an xsl transformation on document in memory. /// </summary> /// <param name="xslFileName">Full pathname of xsl file to use for transformation.</param> /// <param name="doc">Xml document to transform.</param> /// <returns>Transformed xml.</returns> public static XmlReader Transform(string xslFileName, IXPathNavigable doc) { return(XmlHelpers.Transform(xslFileName, doc, null)); }
/// <summary> /// Aggregates xml data defined in different parameter types into a single XmlWriter stream and then returns the data as an XmlReader /// </summary> /// <param name="rootName">The root element name, if null or an empty string is passed, a root element is not created in the output.</param> /// <param name="paramObjects">Supported types are XmlReader, IXPathNavigable, XPathNodeIterator, XmlAttribute, XmlAttributeCollection, and string. String parameters are considered to be raw Xml data.</param> /// <returns></returns> static public XmlReader AggregateXml(string rootName, params object[] paramObjects) { MemoryStream stream = null; try { stream = new MemoryStream(); using (XmlWriter writer = XmlHelpers.CreateXmlWriterForReading(stream)) { if (!string.IsNullOrEmpty(rootName)) { writer.WriteStartElement(rootName); } foreach (object obj in paramObjects) { if (null != obj) { if (obj is XmlReader) { ((XmlReader)obj).MoveToContent(); writer.WriteNode((XmlReader)obj, false); } else if (obj is IXPathNavigable) { IXPathNavigable nav = (IXPathNavigable)obj; writer.WriteNode(nav.CreateNavigator(), false); } else if (obj is string) { writer.WriteRaw((string)obj); } else if (obj is XPathNodeIterator) { XPathNodeIterator iter = (XPathNodeIterator)obj; foreach (XPathNavigator nav in iter) { writer.WriteNode(nav.CreateNavigator(), false); } } else if (obj is XmlAttribute) { XmlAttribute attr = (XmlAttribute)obj; writer.WriteAttributeString(attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value); } else if (obj is XmlAttributeCollection) { XmlAttributeCollection coll = (XmlAttributeCollection)obj; foreach (XmlAttribute attr in coll) { writer.WriteAttributeString(attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value); } } else //throw an exception if it is not one of the supported types { throw new ArgumentException("Unsupported parameter type"); } } } if (!string.IsNullOrEmpty(rootName)) { writer.WriteEndElement(); } } return(XmlHelpers.CreateXmlReaderFromMemoryStream(stream)); } catch (Exception e) { if (stream != null) { stream.Close(); } throw e; } }
/// <summary> /// Perform an xml transformation on a reader. /// </summary> /// <param name="xslFileName">Full path of xsl file to use for transformation.</param> /// <param name="reader">Xml document to transform.</param> /// <returns>Transformed xml.</returns> public static XmlReader Transform(string xslFileName, XmlReader reader) { return(XmlHelpers.Transform(xslFileName, reader, null)); }