Connecting Tech Pros Worldwide Help | Site Map

XmlReaderSettings Conflict with UTF-16?

  #1  
Old November 13th, 2008, 05:25 PM
Daniel S
Guest
 
Posts: n/a
Hi,

Here's my problem code:

....
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Document;
settings.CheckCharacters = false;
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
settings.ProhibitDtd = false;
System.IO.FileStream stream = new System.IO.FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.Read);
XmlReader reader = XmlReader.Create(stream, settings);
XmlDocument myDocument = new XmlDocument();
try
{
myDocument.Load(reader);
}
catch (Exception e)
{
....

The code works if "path" refers to a UTF-8 encoded XML file. If it
refers to a UTF-16 encoded file, myDocument.Load(reader) throws an
exception. Also, if I replace myDocument.Load(reader) with
myDocument.Load(path), the code works for both UTF-8 and UTF-16, but I
can't do that because I need the XmlReaderSettings.

The message and stack trace are:

An error has occurred while opening external DTD 'file:///C:/Documents
and Settings/myName/folder1/.../folder6/folder7/someFileName.dtd':
Could not find a part of the path 'C:\\Documents and Settings\\myName\
\folder1\\...

at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushEx ternalSubset
(String systemId, String publicId)
...

That's not the correct path to the DTD. The path is relative to the
location of the XML file.

The XML contains:

<!DOCTYPE myType SYSTEM "folder6/folder7/someFileName.dtd">

This exact tag appears in UTF-8 encoded XML files in an identical
directory structure, and they don't produce this error.

Any help would be much appreciated.

Thanks,
Daniel Sheiner
  #2  
Old November 13th, 2008, 05:25 PM
Daniel S
Guest
 
Posts: n/a

re: XmlReaderSettings Conflict with UTF-16?


(okay, I used &lt; and &gt; there thinking they'd be turned into < and
Quote:
>. The XML *actually* contains:
<!DOCTYPE myType SYSTEM "folder6/folder7/someFileName.dtd">

Sorry if this caused any confusion.)

  #3  
Old November 13th, 2008, 05:35 PM
Martin Honnen
Guest
 
Posts: n/a

re: XmlReaderSettings Conflict with UTF-16?


Daniel S wrote:
Quote:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Document;
settings.CheckCharacters = false;
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
settings.ProhibitDtd = false;
System.IO.FileStream stream = new System.IO.FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.Read);
XmlReader reader = XmlReader.Create(stream, settings);
Why don't you simply use
XmlReader reader = XmlReader.Create(path, settings);
?
Quote:
XmlDocument myDocument = new XmlDocument();
try
{
myDocument.Load(reader);
}
catch (Exception e)
{
...
>
The code works if "path" refers to a UTF-8 encoded XML file. If it
refers to a UTF-16 encoded file, myDocument.Load(reader) throws an
exception. Also, if I replace myDocument.Load(reader) with
myDocument.Load(path), the code works for both UTF-8 and UTF-16, but I
can't do that because I need the XmlReaderSettings.
>
The message and stack trace are:
>
An error has occurred while opening external DTD 'file:///C:/Documents
and Settings/myName/folder1/.../folder6/folder7/someFileName.dtd':
Could not find a part of the path 'C:\\Documents and Settings\\myName\
\folder1\\...
That message does not look in any way to be related to the encoding of
the XML document.

Quote:
This exact tag appears in UTF-8 encoded XML files in an identical
directory structure, and they don't produce this error.
I will try to reproduce that, can't currently believe that the encoding
should have any effect on URL resolution.




--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #4  
Old November 13th, 2008, 05:55 PM
Martin Honnen
Guest
 
Posts: n/a

re: XmlReaderSettings Conflict with UTF-16?


Martin Honnen wrote:
Quote:
Quote:
>This exact tag appears in UTF-8 encoded XML files in an identical
>directory structure, and they don't produce this error.
>
I will try to reproduce that, can't currently believe that the encoding
should have any effect on URL resolution.
I have tried to reproduce that, using an UTF-8 encoded file and an
UTF-16 encoded file, both referencing the same DTD file in a
subdirectory. I am not able to reproduce that error so check your file
paths, the error very much sounds as if there is indeed a path wrong.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #5  
Old November 13th, 2008, 06:25 PM
Daniel S
Guest
 
Posts: n/a

re: XmlReaderSettings Conflict with UTF-16?


Why don't you simply use
Quote:
XmlReader reader = XmlReader.Create(path, settings);
>?
Good question. I rearranged the code to simplify my earlier post.
The stream was actually created in its own function:

public static System.IO.FileStream AdvanceStreamToStartXml(string
path)
{
System.IO.FileStream stream = new System.IO.FileStream(path,
FileMode.Open, FileAccess.Read, FileShare.Read);
return AdvanceStreamToStartXml(stream);
}

My eyes played a trick on me, transforming that return statement to
"return stream;" (I'm obviously not the original author of this
code). In AdvanceStreamToStartXml(stream), we're advancing the start
position of the FileStream to just past the doctype tag (that
validation is supposed to happen elsewhere), and we do so assuming
single-byte encoding, which I believe is the root of this problem.

Thanks!!!!!!
-Daniel
Closed Thread