all groups > dotnet xml > march 2005 >
You're in the

dotnet xml

group:

XmlTextReader; XmlException


XmlTextReader; XmlException edi
3/19/2005 6:57:20 AM
dotnet xml: Hi,

I have MS .Net Framework v1.1.4322.
I have this XML file:

<?xml version="1.0" ?> <!--here there are two spaces at the
beginning-->
<aaa>
<a id="1">
<Dept>Finance</Dept>
</a>
<a id="2">
<Dept>Marketing</Dept>
</a>
</aaa>

This file opens correctly with Internet Explorer.
When I try to read from it with XmlTextReader the code throws
XmlException.
Here is the code:
XmlTextReader reader=new
XmlTextReader("EmpRec.xml");
try
{
reader.Read();
reader.Read(); //!!!EXCEPTION
}
catch (XmlException e){
Console.WriteLine(e.ToString());
}
Console result:
System.Xml.XmlException: The XML declaration is unexpected. Line 1,
position 5.
at System.Xml.XmlTextReader.ParseTag()
at System.Xml.XmlTextReader.ParseRoot()
at System.Xml.XmlTextReader.Read()
at MyProject.MyForm.ReadX() ...
It works correctly when I remove two spaces at the beginning of the XML
file.
What can I do to remove this problem easily? And why first Read
(reader.Read();) passes?

Thanks
Re: XmlTextReader; XmlException Martin Honnen
3/19/2005 4:23:07 PM


[quoted text, click to view]


[quoted text, click to view]
^^^
The XML declaration has to be at the start of the XML document, it could
only be preceded by a byte order mark (BOM) necessary for instance for
UTF-16 encoded documents.


--

Martin Honnen
AddThis Social Bookmark Button