Use XslTransform class in System.Xml.Xsl ;
Original XML(testCases.xml):
<?xml version="1.0" encoding="utf-8" ?>
<testcases>
<testcase id="001" bvt="yes">
<inputs>
<arg1>red</arg1>
<arg2>blue</arg2>
</inputs>
<expected>purple</expected>
</testcase>
</testcases>
<testcases>
<testcase id="001" bvt="yes">
<inputs>
<arg1>red</arg1>
<arg2>blue</arg2>
</inputs>
<expected>purple</expected>
</testcase>
</testcases>
Transformed XML(transformed.xml):
<?xml version="1.0" encoding="utf-8"?>
<allOfTestCases>
<aCase caseID="001">
<bvt>yes</bvt>
<expRes>purple</expRes>
<inputs>
<input1>red</input1>
<input2>blue</input2>
</inputs>
</aCase>
</allOfTestCases>
<allOfTestCases>
<aCase caseID="001">
<bvt>yes</bvt>
<expRes>purple</expRes>
<inputs>
<input1>red</input1>
<input2>blue</input2>
</inputs>
</aCase>
</allOfTestCases>
XSLT file(transformer.xslt):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<allOfTestCases>
<xsl:for-each select="//testcase">
<aCase>
<xsl:attribute name ="caseID">
<xsl:value-of select ="@id"/>
</xsl:attribute>
<bvt>
<xsl:value-of select ="@bvt"/>
</bvt>
<expRes>
<xsl:value-of select ="expected"/>
</expRes>
<inputs>
<xsl:for-each select ="inputs">
<input1>
<xsl:value-of select ="arg1"/>
</input1>
<input2>
<xsl:value-of select ="arg2"/>
</input2>
</xsl:for-each>
</inputs>
</aCase>
</xsl:for-each>
</allOfTestCases>
</xsl:template>
</xsl:stylesheet>
Program.cs:
using System;
using System.Collections.Generic;
using System.Xml.Xsl ;
namespace TransformXML
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start Transform!");
XslTransform xst = new XslTransform();
xst.Load ("..\\..\\transformer.xslt");
xst.Transform("..\\..\\testCases.xml", "..\\..\\transformed.xml");
Console.WriteLine("Done");
}
}
}
using System.Collections.Generic;
using System.Xml.Xsl ;
namespace TransformXML
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start Transform!");
XslTransform xst = new XslTransform();
xst.Load ("..\\..\\transformer.xslt");
xst.Transform("..\\..\\testCases.xml", "..\\..\\transformed.xml");
Console.WriteLine("Done");
}
}
}