L
Lloyd Dupont
I have a XML document (not of my making) which starts like that:
==========
<?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">
<dict>
.........................
==========
I read it with code such as:
Stream s = .....
using(XmlTextReader xtr = new XmlTextReader(s))
{
xtr.MoveToContent(); // <<== problem here
xr.Read();
.....
}
The problem that I have is, if the user's computer is not connected to a
network, the xtr.MoveToContent() fail with the following exception:
=========
Stack :
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset
(String systemId, String publicId)
at
System.Xml.XmlTextReaderImpl.DtdParserProxy.System.Xml.IDtdParserAdapter
..PushExternalSubset(String systemId, String publicId)
at System.Xml.DtdParser.ParseExternalSubset()
at System.Xml.DtdParser.ParseInDocumentDtd(Boolean
saveInternalSubset)
at System.Xml.DtdParser.Parse(Boolean saveInternalSubset)
at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlReader.MoveToContent()
-------- Caused by Exception --------
Exception : System.Net.WebException
Message : Unable to connect to the remote server
Source : System
Help :
Stack :
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri,
ICredentials credentials)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials
credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String
role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenStream(Uri uri)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset
(String systemId, String publicId)
-------- Caused by Exception --------
Exception : System.Net.Sockets.SocketException
Message : A socket operation was attempted to an unreachable host
Source : System
Help :
Stack :
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean
connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress&
address, ConnectSocketState state, IAsyncResult asyncResult, Int32
timeout, Exception& exception)
=========
But I absolutely don't care about the DTD, in fact there is nothing at this
URL.
I try many various settings, but I can't see a way to get around this
problem other than a try/catch.
The problem is: it is easy to forget to put try/catch around the first XML
read.
Is there any better way?
==========
<?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">
<dict>
.........................
==========
I read it with code such as:
Stream s = .....
using(XmlTextReader xtr = new XmlTextReader(s))
{
xtr.MoveToContent(); // <<== problem here
xr.Read();
.....
}
The problem that I have is, if the user's computer is not connected to a
network, the xtr.MoveToContent() fail with the following exception:
=========
Stack :
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset
(String systemId, String publicId)
at
System.Xml.XmlTextReaderImpl.DtdParserProxy.System.Xml.IDtdParserAdapter
..PushExternalSubset(String systemId, String publicId)
at System.Xml.DtdParser.ParseExternalSubset()
at System.Xml.DtdParser.ParseInDocumentDtd(Boolean
saveInternalSubset)
at System.Xml.DtdParser.Parse(Boolean saveInternalSubset)
at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlReader.MoveToContent()
-------- Caused by Exception --------
Exception : System.Net.WebException
Message : Unable to connect to the remote server
Source : System
Help :
Stack :
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri,
ICredentials credentials)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials
credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String
role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenStream(Uri uri)
at System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset
(String systemId, String publicId)
-------- Caused by Exception --------
Exception : System.Net.Sockets.SocketException
Message : A socket operation was attempted to an unreachable host
Source : System
Help :
Stack :
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean
connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress&
address, ConnectSocketState state, IAsyncResult asyncResult, Int32
timeout, Exception& exception)
=========
But I absolutely don't care about the DTD, in fact there is nothing at this
URL.
I try many various settings, but I can't see a way to get around this
problem other than a try/catch.
The problem is: it is easy to forget to put try/catch around the first XML
read.
Is there any better way?