Hello !
I was developing xslt sheet lately and - experimenting - I made
mistake resulting in that, the effect of the transformation is not
well-formed xml document.
I made these tests using XmlSpy and in output window I can see two
parallel elements - according to "logic" of me sheet. The error of
that document is that it doesn't have root element.
Here is the contents of XmlSpy output window:
<?xml version="1.0" encoding="UTF-8"?>
<child name="Peter" age="200">
<parent names="Eva" age="10090">
<children>
<child name="Peter" age="200"/>
<child name="Barbara" age="100"/>
</children>
</parent>
</child>
<child name="Barbara" age="100">
<parent names="Eva" age="10090">
<children>
<child name="Peter" age="200"/>
<child name="Barbara" age="100"/>
</children>
</parent>
</child>
I made small C# program intended to make xslt transformation for a
given xml input and xslt sheet. Here is my code:
using System;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
namespace XsltTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length!=2)
{
Console.WriteLine("XsltTest InputDoc.Xml TransSheet.xsl");
return ;
}
else
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(args[0]);
MemoryStream memStream = new MemoryStream(1000);
XmlTextWriter xWriter = new XmlTextWriter(memStream,
System.Text.Encoding.UTF8);
xWriter.Formatting = Formatting.Indented;
XslTransform xTrans = new XslTransform();
xTrans.Load(args[1]);
xTrans.Transform(xDoc,null, xWriter, null);
xWriter.Flush();
memStream.Position = 0;
XmlTextReader xReader = new XmlTextReader(memStream);
xReader.MoveToContent();
XmlDocument xDocResult = new XmlDocument();
xDocResult.LoadXml(xReader.ReadOuterXml());
xDocResult.Save(args[0]+".xml");
}
}
}
}
I know there is possible to use other approach (Transform method has
18 overloads !!!!) but I want to have result of transformation as an
XmlDocument object. Save (in last line of the code) is for testing
purposes only.
But here comes my problem. The contents of the written file is only
the first element from results of transformation:
<child name="Peter" age="200">
<parent names="Eva" age="10090">
<children>
<child name="Peter" age="200">
</child>
<child name="Barbara" age="100">
</child>
</children>
</parent>
</child>
Is something wrong with my code, or rather XmlDocument .Net has some
strange behaviour when loading not well-formed xml ?