471,049 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

XslCompiledTransform Load method getting "Root element is missing" XSL Compile error...???


XslCompiledTransform xslt = new XslCompiledTransform();

ASCIIEncoding encodeToByteArray = new ASCIIEncoding();
MemoryStream inputStream = new MemoryStream();
MemoryStream outputStream = new MemoryStream();
MemoryStream xslStream = new MemoryStream();

String inputString = "<foo>Hello XSL</foo>";
byte[] inputArray =
encodeToByteArray.GetBytes(inputString);
//write the string buffer to the stream
inputStream.Write(inputArray, 0, inputString.Length);

String xslString = "<xsl:stylesheet
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
version=\"1.0\"><xsl:template match=\"foo\"><out><xsl:value-of
select=\".\"/></out></xsl:template></xsl:stylesheet>";
byte[] xslArray = encodeToByteArray.GetBytes(xslString);

//write the string buffer to the stream
xslStream.Write(xslArray, 0, xslString.Length);

xslt.Load(xslStream); <-- exception here

what's wrong?

here is xslString
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="foo">
<out><xsl:value-of select="."/></out>
</xsl:template>
</xsl:stylesheet>

Dec 7 '06 #1
2 8170
crap figured it out..i didn't reset the stream back to the beginning =P

xslStream.Write(xslArray, 0, xslString.Length);

xslStream.Seek(0, SeekOrigin.Begin);

xslt.Load(xslStream); <-- exception here

tony wrote:
XslCompiledTransform xslt = new XslCompiledTransform();

ASCIIEncoding encodeToByteArray = new ASCIIEncoding();
MemoryStream inputStream = new MemoryStream();
MemoryStream outputStream = new MemoryStream();
MemoryStream xslStream = new MemoryStream();

String inputString = "<foo>Hello XSL</foo>";
byte[] inputArray =
encodeToByteArray.GetBytes(inputString);
//write the string buffer to the stream
inputStream.Write(inputArray, 0, inputString.Length);

String xslString = "<xsl:stylesheet
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
version=\"1.0\"><xsl:template match=\"foo\"><out><xsl:value-of
select=\".\"/></out></xsl:template></xsl:stylesheet>";
byte[] xslArray = encodeToByteArray.GetBytes(xslString);

//write the string buffer to the stream
xslStream.Write(xslArray, 0, xslString.Length);

xslt.Load(xslStream); <-- exception here

what's wrong?

here is xslString
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="foo">
<out><xsl:value-of select="."/></out>
</xsl:template>
</xsl:stylesheet>
Dec 8 '06 #2
tony wrote:
XslCompiledTransform xslt = new XslCompiledTransform();

ASCIIEncoding encodeToByteArray = new ASCIIEncoding();
MemoryStream inputStream = new MemoryStream();
MemoryStream outputStream = new MemoryStream();
MemoryStream xslStream = new MemoryStream();

String inputString = "<foo>Hello XSL</foo>";
byte[] inputArray =
encodeToByteArray.GetBytes(inputString);
//write the string buffer to the stream
inputStream.Write(inputArray, 0, inputString.Length);

String xslString = "<xsl:stylesheet
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
version=\"1.0\"><xsl:template match=\"foo\"><out><xsl:value-of
select=\".\"/></out></xsl:template></xsl:stylesheet>";
byte[] xslArray = encodeToByteArray.GetBytes(xslString);

//write the string buffer to the stream
xslStream.Write(xslArray, 0, xslString.Length);

xslt.Load(xslStream); <-- exception here

what's wrong?
The whole approach witht those memory streams is cumbersome and
unnecessary. If you have a string with XML (or XSLT) then simply use a
StringReader over that string and pass the StringReader or XmlReader
over that StringReader to the method e.g.
xslt.Load(XmlReader.Create(new StringReader(xslString)));
You do not gain anything by wrestling with encodings, byte arrays and
streams, all those XML APIs consume XmlReader or even StringReader directly.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Dec 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by will | last post: by
reply views Thread by damontimm | last post: by
3 posts views Thread by lisa.bogart | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.