Geeks With Blogs
David Douglass .NET on My Mind

The following code doesn't work; the exception message reads "Root element is missing.":

MemoryStream transformed = new MemoryStream(); XmlDocument doc = new XmlDocument(); XmlReader input = null; XslCompiledTransform xslt = new XslCompiledTransform(); input = XmlReader.Create("Example.xml"); xslt.Load("Example.xslt"); xslt.Transform(input, new XsltArgumentList(), transformed); try { doc.Load(transformed); } catch (Exception ex) { Console.WriteLine("XmlDocument.Load failed: {0}", ex.Message); } Console.ReadLine();

It isn't a problem with the input file or the transform script.  The problem is that the XslCompiledTransform.Transform function leaves the stream position set to the end of the stream.  There's no root element because the XmlDocument.Load function thinks the stream has a 0 byte length.  The fix is to simply set the position to 0:

MemoryStream transformed = new MemoryStream(); XmlDocument doc = new XmlDocument(); XmlReader input = null; XslCompiledTransform xslt = new XslCompiledTransform(); input = XmlReader.Create("Example.xml"); xslt.Load("Example.xslt"); xslt.Transform(input, new XsltArgumentList(), transformed); // THE FIX transformed.Position = 0; try { doc.Load(transformed); } catch (Exception ex) { Console.WriteLine("XmlDocument.Load failed: {0}", ex.Message); } Console.ReadLine();
Posted on Thursday, November 15, 2007 7:23 AM | Back to top


Comments on this post: Root element is missing.

# re: Root element is missing.
Requesting Gravatar...
Thanks, that one got me
Left by JB on Jun 03, 2008 3:31 PM

# re: Root element is missing.
Requesting Gravatar...
Thanks, solved my problem too.
Left by Eric Lindberg on Jun 16, 2008 9:34 PM

# re: Root element is missing.
Requesting Gravatar...
This was helpful... Thanks
Left by Jon on Oct 10, 2008 4:32 PM

# re: Root element is missing.
Requesting Gravatar...
You the man!
Left by M on Dec 10, 2008 4:32 AM

# re: Root element is missing.
Requesting Gravatar...
Thanks a bunch, this worked nicely for me
Left by FrankC on Dec 13, 2008 3:45 PM

# re: Root element is missing.
Requesting Gravatar...
Hi folks,

Which file do I need to modify in order to fix the problem?

I'm very new to this .net stuff so I wouldn't know what to do.

thanks,
Left by RL on Mar 11, 2009 12:52 PM

# re: Root element is missing.
Requesting Gravatar...
Excellent post man, thanks. This one got me too. Spent 15 minutes peering at the XML through watery eyes before I let Google do the work.
Left by Mark on Mar 25, 2009 1:45 PM

# re: Root element is missing.
Requesting Gravatar...
Thanks, it works again. BING saved the day
Left by fredrik on Oct 05, 2009 9:35 AM

# re: Root element is missing.
Requesting Gravatar...
Thank you so much, kind sir.
Left by CF on Oct 08, 2009 3:01 PM

# re: Root element is missing.
Requesting Gravatar...
Thanks a lot!!!
Left by lostintrans on Nov 23, 2009 8:53 PM

# re: Root element is missing.
Requesting Gravatar...
I get the "Root element is missing." only in certain PCs.Can it have any relation to the PC settings?
Left by shadow on Nov 26, 2009 1:08 AM

# re: Root element is missing.
Requesting Gravatar...
Almost a day trying to find what was wrong.... thanks!!
Left by Rafael on Feb 11, 2010 6:29 AM

# re: Root element is missing.
Requesting Gravatar...
Thanks! Very helpful.
Left by Boris Gomiunik on Mar 08, 2010 1:57 AM

# re: Root element is missing.
Requesting Gravatar...
That fixed it for me too. Thanks!
Left by David on Apr 02, 2010 8:06 PM

# re: Root element is missing.
Requesting Gravatar...
thanks problem fixed
Left by binx on May 19, 2010 7:10 AM

# re: Root element is missing.
Requesting Gravatar...
having a problem of root element is missing whil booking e-ticket in tatkal quota on 8:00 AM.
Left by BIPIN JAISWAL on May 27, 2010 3:22 AM

# re: Root element is missing.
Requesting Gravatar...
What if transformed is type stream instead of MemoryStream in which the position could not be set? What is the best approach in solving this?

Thanks
Left by gka on Jun 01, 2010 5:44 AM

# re: Root element is missing.
Requesting Gravatar...
You Rock!!!
Left by lekan on Jul 23, 2010 6:56 AM

# re: Root element is missing.
Requesting Gravatar...
this is helpful for me as well! thanks!
Left by voodoo on Jul 27, 2010 5:18 AM

# re: Root element is missing.
Requesting Gravatar...
Thanks! This should be in the manual!
Left by Philip Bliss on Oct 12, 2010 8:16 PM

# re: Root element is missing.
Requesting Gravatar...
Thanks for your blog
Left by Reddy Naresh on Mar 25, 2011 11:00 AM

# re: Root element is missing.
Requesting Gravatar...
sol home page ki demand leter side open nh ho rhi..
Left by inderbala on Jan 02, 2012 3:30 AM

# re: Root element is missing.
Requesting Gravatar...
Perfect, thanks!
Left by Rahul on May 14, 2012 8:40 PM

# re: Root element is missing.
Requesting Gravatar...
Lost half a day on this!
Thanks David!
Left by Adam on Aug 06, 2012 10:40 AM

Your comment:
 (will show your gravatar)


Copyright © David Douglass | Powered by: GeeksWithBlogs.net