I get this error when calling DataSet.ReadXml(stream). I am passing a
memorystream that was passed to XslCompiledTransformer.Transform(). The
resulting stream has an unprintable character just before the xml declaration.
After a couple of hours I found the issues and decided to use the following
workaround:
string x = UTF8Encoding.UTF8.GetString(transformedXml.ToArray());
webServerDataSet.ReadXml(new StringReader(x.Trim()));
********original code**********
DataSet webServerDataSet = null;
XPathDocument xslt;
XPathDocument xml;
XslCompiledTransform xslTransformer;
MemoryStream transformedXml;
bool blnFilter = false;
try
{
string webServerXML =
AWebService.GetWebServers(sourceServerId, blnFilter);
if (!string.IsNullOrEmpty(webServerXML))
{
//retrieve xslt from embedded resource
xslt = new XPathDocument(new
StringReader(Properties.Resources.WebServerXslt));
//load webServerXML into an XPathDocument
xml = new XPathDocument(new
System.IO.StringReader(webServerXML.Trim()));
//load transformer engin
xslTransformer = new XslCompiledTransform();
xslTransformer.Load(xslt);
using (transformedXml = new MemoryStream() )
{
//do the transformation and output into the
memorystream
xslTransformer.Transform(xml, null, transformedXml);
//load our grouped and sorted xml into a dataset
webServerDataSet = new DataSet();
string x =
UTF8Encoding.UTF8.GetString(transformedXml.ToArray());
webServerDataSet.ReadXml(new StringReader(x.Trim()));
}
.....
***************************
*********xslt**************
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<!--when used with translate() function will change case-->
<xsl:param name="LCASE" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:param name="UCASE" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<!--keys for grouping-->
<xsl:key name="webserver-by-Comment" match="webserver"
use="translate(@comment,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:template match="/webservers">
<xsl:element name="webservers">
<xsl:for-each select="webserver[generate-id() =
generate-id(key('webserver-by-Comment',translate(@comment,$LCASE,$UCASE))[1])]">
<xsl:sort order="ascending" data-type="text" select="@comment"/>
<xsl:variable name="grp"
select="translate(string(@comment),$UCASE,$LCASE)"/>
<xsl:for-each
select="key('webserver-by-Comment',translate(@comment,$LCASE,$UCASE))[1]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
*************************
--
kevin...
Which error exactly do you get? If the error is "root element is
missing" then you can easily fix that by setting Position to 0 after the
transformation but before you load into the data set e.g.
using (transformedXml = new MemoryStream() )
{
//do the transformation and output into the
memorystream
xslTransformer.Transform(xml, null,
transformedXml);
transformedXml.Position = 0;
If there are problems with characters before the XML declaration then
that can only be a BOM and that should not pose any problems. So try
setting Position to 0 on the memory stream before you pass it to the
ReadXml method. If you still have problems then post the exact error
message.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
> Which error exactly do you get?
System.Xml.XmlExceptio.message = ""Root element is missing.""
> If the error is "root element is
> missing" then you can easily fix that by setting Position to 0 after the
> transformation but before you load into the data set e.g.
That's a thought.
Is that character due to something I am doing, either in the xslt or code,
that i should not be doing?
Kevin
> Is that character due to something I am doing, either in the xslt or code,
> that i should not be doing?
The problem is that you write to the stream and that way it is
positioned at the end, then you try to read from the end of the stream.
So set Position to 0 and all is fine, the ReadXml method will then find
the root element.
That character before the XML declaration does not cause the error
message "root element is missing". It is the BOM and should not hurt.
Duh!!!
Thanks Martin.
Kevin