XML reader issues....

Discussion in 'ASP .Net' started by Lloyd Dupont, Apr 17, 2007.

  1. Lloyd Dupont

    Lloyd Dupont Guest

    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?
     
    Lloyd Dupont, Apr 17, 2007
    #1
    1. Advertisements

  2. 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 [MVP], Apr 17, 2007
    #2
    1. Advertisements

  3. Lloyd Dupont

    Lloyd Dupont Guest

    Thanks Oleg

     
    Lloyd Dupont, Apr 17, 2007
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.