.NETGURU
XmlTextReader strangeness
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.sdk.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Todd Bright (VIP)
Got a bit of strangeness going on when assigning an XML stream to an XmlTextReader. It's actually a MemoryStream representing a well-formed ASCII XML document (I've confirmed this). The following code snippet works...

XmlTextReader tr = new XmlTextReader(stream);
stream.Position = 0; // this is the key to making it work
tr.Read();

.... and the next code snippet throws the error "root element is missing"...

XmlTextReader tr = new XmlTextReader(stream);
//stream.Position = 0;
tr.Read();

If I save the stream to a file, then initialize the XmlTextReader object with the filename everything works fine. I am actually using the XmlTextReader object to validate the Xml against an XSD. Here's the complete code that I'm using...

XmlTextReader tr = new XmlTextReader(stream);
XmlValidatingReader reader = new XmlValidatingReader(tr);
reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
reader.ValidationType = ValidationType.Schema;
stream.Position = 0; // this MUST be set to 0
XmlDocument x = new XmlDocument();
x.Load(reader); // if stream.Position isn't explicitly set to 0 I get the "root element
// is missing" error on this call

It's strange that I can set stream.Position = 0 anywhere before the x.Load() call, but it DOES have to be set (or I get the error stated above). The following code works without problem...

XmlValidatingReader reader = new XmlValidatingReader(stream,
System.Xml.XmlNodeType.Element, null);
reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
reader.ValidationType = ValidationType.Schema;
XmlDocument x = new XmlDocument();
x.Load(reader);

If the XmlTextReader is taken out and the stream is passed directly into the XmlValidatingReader everything works as you would think it should. I know this was fairly lengthy but hopefully easy to follow. Anyone out there have any idea why this may be happening???

My thoughts...

As stated in code snippet #3, it doesn't matter where stream.Position=0 is set, just as long as it's set before the x.Load() call. Maybe the XmlTextReader object doesn't initialize the stream to the first position when loaded and since the stream is referenced by address in .Net in all sub-objects, where stream.Position is set doesn't matter? The XmlValidatingReader, though, does seem to initilize the stream correctly. Any thoughts?

Reply to this message...
 
    
GraemeBryce
I have a very similar issue. It is of note that the error can only be
replicated on Windows Server 2004 and not on earlier server OS
version.

It is indeed odd

Many thanks for the fix.

Graeme

Todd Bright wrote:
[Original message clipped]

--
GraemeBryce
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message772754.html

Reply to this message...
 
 
System.IO.MemoryStream
System.Xml.Schema.ValidationEventHandler
System.Xml.ValidationType
System.Xml.XmlDocument
System.Xml.XmlNodeType
System.Xml.XmlTextReader
System.Xml.XmlValidatingReader




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification