Home All Groups Group Topic Archive Search About

XML reader issues....

Author
17 Apr 2007 12:30 AM
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?

Author
17 Apr 2007 6:34 AM
Oleg Tkachenko [MVP]
Lloyd Dupont wrote:
Show quote
> 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".


Author
17 Apr 2007 7:06 AM
Lloyd Dupont
Thanks Oleg

Show quote
"Oleg Tkachenko [MVP]" <s***@body.com> wrote in message
news:%23swQjoLgHHA.1220@TK2MSFTNGP03.phx.gbl...
> Lloyd Dupont wrote:
>> 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".
>
>
> --
> Oleg Tkachenko [XML MVP, MCPD]
> http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net

AddThis Social Bookmark Button