XML reader issues....

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?
 
O

Oleg Tkachenko [MVP]

Lloyd said:
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.

XmlReader always checks if DTD exists (to make sure document is
wellformed), it doesn't actually parse it. So the solution is to provide
to XmlReader custom XmlResolver, which will return something dummy when
requested to resolve "http://www.apple.com/DTDs/PropertyList-1.0.dtd".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top