I am trying to transform XML to XML using an XSLT in C#, but the root node
of my XML is not being matched by the XSLT if it has an xmlns attribute. Am
I handling my namespaces incorrectly?
My C# code is,
// Create an XSLT transform object
XslTransform xslTransform = new XslTransform();
// Load the stylesheet
xslTransform.Load(xsltFilename);
// Create a new XPathDocument and load the XML data to be transformed.
// (string xmlString contains the XML below)
XPathDocument xPathDocument = new XPathDocument(xmlString);
// Create a MemoryStream to receive the data
MemoryStream transformedXmlStream = new MemoryStream();
// Transform the file
xslTransform.Transform(xPathDocument, null, transformedXmlStream, null);
// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding();
string outputXmlString = ascii.GetString(transformedXmlStream.GetBuffer());
my XML string is,
<?xml version="1.0" encoding="utf-8" ?>
<CalculatorParameters xmlns="http://www.kettley.com/CalculatorParameters">
<ChildNode>TextValue</ChildNode>
</CalculatorParameters>
my XSD is,
<?xml version="1.0"?>
<xs:schema id="NewDataSet"
targetNamespace="http://www.kettley.com/CalculatorParameters"
xmlns:mstns="http://tempuri.org/XMLFile1.xsd"
xmlns="http://tempuri.org/XMLFile1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="CalculatorParameters">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildNode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="CalculatorParameters" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and my XSLT is,
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes"
encoding="UTF-8"/>
<xsl:template match = "/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="CalculatorParameters">
<AnyResult />
</xsl:template>
</xsl:stylesheet>
My problem is that the root CalculatorParameters node is only matched if I
remove its
xmlns="http://www.kettley.com/CalculatorParameters"attribute from it.
A second glitch is that, the contents of the output string have what look
like junk characters, i.e.,
"o;?<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<AnyResult />"with a whole
bunch of '\0' characters appended to the end. Where are the leading "o;?"
and trailing '\0' characters coming from?
This is really my first time working with XML much or in C# so I am a bit
wobbly.