Use hard coded xsl to convert xml document : Nodes « XML LINQ « C# / C Sharp






Use hard coded xsl to convert xml document

  

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass
{
    public static void Main()
    {
        string xslMarkup = @"<?xml version='1.0'?>
        <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
            <xsl:template match='/Parent'>
                <Root>
                    <C1>
                    <xsl:value-of select='A1'/>
                    </C1>
                </Root>
            </xsl:template>
        </xsl:stylesheet>";

        XDocument xmlTree = new XDocument(
            new XElement("Parent",
                new XElement("A1", "A1 data"),
                new XElement("A2", "A2 data")
            )
        );

        XDocument newTree = new XDocument();
        using (XmlWriter writer = newTree.CreateWriter())
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(XmlReader.Create(new StringReader(xslMarkup)));

            xslt.Transform(xmlTree.CreateReader(), writer);
        }

        Console.WriteLine(newTree);
    }
}

   
    
  








Related examples in the same category

1.Calling the Only Nodes Prototype
2.Traversing Backward from an XElement Object via the PreviousNode Property
3.Adds the specified content immediately after this node.
4.Adds the specified content immediately before this node.
5.Returns a collection of the ancestor elements of this node.
6.Returns a filtered collection of the ancestor elements of this node.
7.Creates an XmlReader for this node.
8.Returns a collection of the sibling elements after this node, in document order.
9.Returns a filtered collection of the sibling elements after this node, in document order.
10.Returns a collection of the sibling elements before this node, in document order.
11.Returns a filtered collection of the sibling elements before this node, in document order.
12.Determines if the current node appears after a specified node in terms of document order.
13.Determines if the current node appears before a specified node in terms of document order.
14.Gets the next sibling node of this node.
15.Returns a collection of the sibling nodes after this node, in document order.
16.Returns a collection of the sibling nodes before this node, in document order.
17.Gets the previous sibling node of this node.
18.Removes this node from its parent.
19.Replaces this node with the specified content.
20.Returns the XML for this node, optionally disabling formatting.
21.Returns the indented XML for this node.
22.Writes this node to an XmlWriter.