Groups | Blog | Home
all groups > dotnet xml > march 2005 >

dotnet xml : Exception on XmlDocumet.Load()


Chris
3/20/2005 12:00:00 AM
Hi,

I have xml files which contain a <!DOCTYPE> with a link to a dtd. On my
local computer this this link is invalid as it refers to a folder that
does not exist. The link has the format "file://localhost...".
Using an System.Xml.XmlDocument object and calling the Load() function,
the parses hangs for about 5 minutes and then thows an Exception with
the text "the semaphore timeout period has expired".
As the MSDN states the XmlDocument.Load() method does not do dtd
validation so I wonder what is happening. Does the parser try to load
the dtd file and gets a network timeout?
Are there any switches in XmlDocument that I can use to ignore the dtd?

Thanks,
Martin Honnen
3/21/2005 12:47:49 PM


[quoted text, click to view]


[quoted text, click to view]

You would need to assign an XmlResolver that returns null for any URL
passed in e.g. (pseudo code)

class DummyResolver : XmlUrlResolver {
public override Uri ResolveUri (Uri baseUri, String relativeUri) {
return null;
}
}

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.XmlResolver = new DummyResolver();

xmlDocument.Load(@"whatever.xml");

But you need to be aware that a DTD is not only necessary for validation
but can also be needed to resolve entity references.

And the behavior of XmlDocument and its XmlResolver depends on different
factors, see
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassXmlResolverTopic.asp>

--

Martin Honnen
Ion Vasilian
3/22/2005 2:44:25 PM
Hi,

Instead of returing 'null' from DummyResolver.ResolveUri(), try returning an
empty stream from DummyResolver.GetEntity() whenever you get a
resolution request for any uri that you don't want resolved. This should
solve your problem.

Ion

[quoted text, click to view]
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemxmlxmlresolverclassresolveuritopic.asp>
[quoted text, click to view]

Ion Vasilian
3/22/2005 2:48:30 PM
Hi,

Instead of returning 'null' from ResolveUri(), return an empty stream
from GetEntity() whenever you get a resolution request for the known
'bad' uris. Regards,

Ion

[quoted text, click to view]
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfSystemXmlXmlDocumentClassXmlResolverTopic.asp>
[quoted text, click to view]

Chris
3/22/2005 5:37:50 PM
Thanks for your reply,
using your suggested method seems the right way.
Doing so, I get an Exception "object reference not set to an instance of
an object" as soon as I 'return null' in the overridden method.
The ResolveUri function is also often called with the document parsed,
but with an empty baseUri. So I think that the baseUri is only != null
when the parser tries to resolve a link. So I tried:

if( baseUri != null )
return null;
else
return base.ResolveUri( baseUri, relativeUri );

But as soon as I return null I get the above mentioned error.
Any suggestions?
Thanks,
Chris

[quoted text, click to view]
Martin Honnen
3/22/2005 6:47:10 PM


[quoted text, click to view]


[quoted text, click to view]

I am not sure how to solve that, looking at the documentation I had
hoped my suggestion would work out but it seems it doesn't, the
documentation here
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlresolverclassresolveuritopic.asp>
explicitly says: "Return Value
A Uri representing the absolute URI or a null reference (Nothing in
Visual Basic) if the relative URI can not be resolved."
Therefore I hoped if the resolver returns null the code calling it would
not further try to deal with the URI and simply ignore it and not try to
load it but that is not the case it seems.


--

Martin Honnen
Chris
3/24/2005 12:14:37 AM
Hi Ion,
thanks a lot for your reply. As I am quite new to .net I am not sure
what type of System.IO.Stream object to retun. Can you give me an
example of an empty stream?

Thanks,
Chris

[quoted text, click to view]
Ion Vasilian
3/24/2005 12:24:13 PM
Hi,

Here's the code that should do the trick:

class DummyResolver : XmlUrlResolver {
....
public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn) {
if (absoluteUri != null && absoluteUri.AbsoluteUri == "<whatever you want
to override>") {
if (ofObjectToReturn == null || ofObjectToReturn ==
typeof(System.IO.Stream)) {
return Stream.Null;
}
}
return base.GetEntity(absoluteUri, role, ofObjectToReturn);
}

Regards,
Ion

[quoted text, click to view]

Chris
3/25/2005 11:09:45 AM
Hi Ion,
thanks a lot for your help!

Regads,
Chris


[quoted text, click to view]
AddThis Social Bookmark Button