Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


"root element missing" after transforming into memorystream

From: kevin@-----------.---------.---
To: NULL
Date: 12/28/2007 9:51:00 AM

using .net 2.0

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...


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent