|
Support
Description So you've got a .NET application that uses System.Xml.XmlTextReader or System.Xml.XmlDocument
and it throws an exception as soon as it loads up your local XML file that goes something like this:
An unhandled exception of type 'System.Net.WebException' occurred in system.dll
Additional information: The underlying connection was closed: The remote name could not be resolved.
This happened to me. Even
Microsoft's article
does not supply the information needed to solve this bug. So I'm here to tell you what's going on.
Your XML document has a link in it that looks up a DTD on the web.
My XML Document looked like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
...
As you can see, the DTD resides on Apple's server which is unfortunately inaccessible when
my laptop is not plugged into the internet. Thus causing an exception.
Solution Well, you can either:
- Comment out or remove the DOCTYPE from the XML file. Or...
- Skip a few lines before the XmlTextReader gets a hold of your document
I personally chose option 2, even though its a hack. And I'd really like it if Microsoft would
allow me to disable the functionality of the XmlTextReader from going out and visiting sites I didn't ask it to
visit. I really couldn't care less if the document is valid according to the DTD. And I'd
rather my program work on my client's computers when they are not connected to the internet.
So here's some C# code to fix the problem:
// create a stream reader
System.IO.StreamReader s = new System.IO.StreamReader(filename);
// Skip the first two lines of the xml file or else it will throw an exception
// when it can't load the DTD from the Internet
s.ReadLine();
s.ReadLine();
// now actually start up the XmlTextReader
System.Xml.XmlTextReader x = new System.Xml.XmlTextReader(s);
I hope this helped you. I searched MSDN and Google for the answer, until I just gave up and figured
it out on my own.
- Robert Wallis
|