Multimobile Development: Building Applications for any Smartphone
XmlValidatingReader, XmlResolver, Proxy Authentication, Credentials, Remote schema
Messages   Related Types
This message was discovered on microsoft.public.dotnet.xml.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Taco
GOOD ANSWER
This is not a problem (anymore), I just want to share my finding.

With kind regards,
Taco Ditiecher.

Problem
=======
You're behind a firewall and want to use specify the credentials to
validate some xml against a schema on a remote server.

Symptoms
========
The followin Exception is thrown:
System.Net.WebException: the remote server returned an error: (407)
Proxy Authentication Required.

The stacktrace contains lines like
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.Schema.DtdParser.ParseDocTypeDecl()
at System.Xml.Schema.DtdParser.Parse()
at System.Xml.XmlTextReader.ParseDtd(XmlScanner scanner)
at System.Xml.XmlTextReader.ParseTag()
at System.Xml.XmlTextReader.ParseRoot()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlValidatingReader.ReadWithCollectTextToken()
at System.Xml.XmlValidatingReader.Read()

Solution
========
The following thread pointed me to the right direction:
http://groups.google.com/groups?threadm=OQhL33hUDHA.1744%40TK2MSFTNGP12.phx.gbl

Instead of having a local copy of the DTD, I used a new
HttpWebRequest, specifying the proxy.

Source code
===========
string xml =
@"<!DOCTYPE root SYSTEM ""http://nowhere.com/my.dtd";">
<root>
<foo/>
<bar/>
</root>";
XmlReader reader = new XmlTextReader(new StringReader(xml));

XmlValidatingReader vr = new XmlValidatingReader(reader);

// The following line will generate an exception.
// vr.XmlResolver = new XmlUrlResolver();
// And this did the trick for me:
XmlUrlProxyResolver myResolver = new XmlUrlProxyResolver();
ICredentials myCred = new NetworkCredential("user", "password",
"domain");
myResolver.Proxy = new WebProxy("http://proxyserver";, true, new
string[]{}, myCred);

// Load the document.
XPathDocument doc = new XPathDocument(vr);

// Custom XmlResolver class.
using System.Net;
public class ProxyXmlUrlResolver : System.Xml.XmlUrlResolver {
public IWebProxy Proxy = WebProxy.GetDefaultProxy();

public override object GetEntity(Uri absoluteUri, string role,
Type ofObjectToReturn) {
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create(absoluteUri);
req.Proxy = Proxy;
return req.GetResponse().GetResponseStream();
}
}
Reply to this message...
Vote that this is a GOOD answer... (87 votes from other users already)
 
 
    
Daniel Cazzulino [MVP] (VIP)
GOOD ANSWER
Cool approach. However, I'd rather use the custom resolver to return a local
copy of the schema. You don't want the schema to be pulled from the inet
everytime you load the document, do you?
This is a problem that XML Catalogs try to solve.

--
Daniel Cazzulino [MVP XML]
Clarius Consulting SA
http://weblogs.asp.net/cazzu
http://aspnet2.com
"Taco" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (85 votes from other users already)
 
 
    
Taco
GOOD ANSWER
Somewhere (I believe on GotDotNet) I found a caching XmlResolver and I
combined that source code with mine. Now it pulls the schema from the
remote server once and stores it locally for later use. It still needs
some finetuning... in case of a validation error I want the resolver
to get a new version of the schema and try to validate again, any
experience with something like that?

Taco.

"Daniel Cazzulino [MVP]" <Click here to reveal e-mail address> wrote in message news:<u6el#Click here to reveal e-mail address>...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (91 votes from other users already)
 
 
 
System.IO.StringReader
System.Net.HttpWebRequest
System.Net.ICredentials
System.Net.IWebProxy
System.Net.NetworkCredential
System.Net.WebException
System.Net.WebProxy
System.Xml.XmlReader
System.Xml.XmlResolver
System.Xml.XmlTextReader
System.Xml.XmlUrlResolver
System.Xml.XmlValidatingReader
System.Xml.XPath.XPathDocument




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734