How to validate xml using a .dtd via a proxy and NOT using system.net.defaultproxy?

Up vote 2 down vote favorite share g+ share fb share tw.

Someone else has already asked a somewhat similar question: stackoverflow.com/questions/1888887/vali... Here's my problem: We have a website application that needs to use both internal and external resources. We have a bunch of internal webservices. Requests to the CANNOT go through the proxy.

If we try to, we get 404 errors since the proxy DNS doesn't know about our internal webservice domains. We generate a few xml files that have to be valid. I'd like to use the provided dtd documents to validate the xml.

The dtd urls are outside our network and MUST go through the proxy. Is there any way to validate via dtd through a proxy without using system.net. Defaultproxy?

If we use defaultproxy, the internal webservices are busted, but the dtd validation works. # Here is what I'm doing to validate the xml right now: public static XDocument ValidateXmlUsingDtd(string xml) { var xrSettings = new XmlReaderSettings { ValidationType = ValidationType. DTD, ProhibitDtd = false }; var sr = new StringReader(xml.Trim()); XmlReader xRead = XmlReader.

Create(sr, xrSettings); return XDocument. Load(xRead); } Ideally, there would be some way to assign a proxy to the XmlReader much like you can assign a proxy to the HttpWebRequest object. Or perhaps there is a way to programatically turn defaultproxy on or off?

So that I can just turn it on for the call to Load the Xdocument, then turn it off again? FYI - I'm open to ideas on how to tackle this - note that the proxy is located in another domain, and they don't want to have to set up a dns lookup to our dns server for our internal webservice addresses. Cheers, Lance c# xml proxy dtd link|improve this question asked May 4 '10 at 14:52Lanceomagnifico476210 33% accept rate.

Yes, you can fix this. One option is to create your own resolver that handles the DTD resolution. It can use whatever mechanism it likes, including employing a non-default proxy for outbound communications.

Var xmlReaderSettings = new XmlReaderSettings { ProhibitDtd = false, ValidationType = ValidationType. DTD, XmlResolver = new MyCustomDtdResolver() }; In the code for MyCustomDtdResolver, you'd specify your desired proxy setting. It could vary depending on the DTD.

You didn't specify, but if the DTDs you are resolving against are fixed and unchanging, then Silverlight and . NET 4.0 have a built-in resolver that does not hit the network (no proxy, no http comms whatsoever). It's called XmlPreloadedResolver.

Out of the box it knows how to resolve RSS091 and XHTML1.0. If you have other DTDs, including your own custom DTDs, and they are fixed or unchanging, you can load them into this resolver and use it at runtime, and completely avoid HTTP comms and the proxy complication. More on that.

If you are not using . NET 4.0, then you can build a "no network" resolver yourself. To avoid the W3C traffic limit, I built a custom resolver myself, for XHTML, maybe you can re-use it.

See also, a related link. For illustration, here's the code for ResolveUri in a custom Uri resolver. /// /// Resolves URIs.

/// /// /// /// The only Uri's supported are those for W3C XHTML 1.0. /// /// public override Uri ResolveUri(Uri baseUri, string relativeUri) { if (baseUri == null) { if (relativeUri. StartsWith("http://")) { Trace(" returning {0}", relativeUri); return new Uri(relativeUri); } // throw if Uri scheme is unknown/unhandled throw new ArgumentException(); } if (relativeUri == null) return baseUri; // both are non-null var uri = baseUri.

AbsoluteUri; foreach (var key in knownDtds. Keys) { // look up the URI in the table of known URIs var dtdUriRoot = knownDtdskey; if (uri. StartsWith(dtdUriRoot)) { string newUri = uri.

Substring(0,dtdUriRoot. Length) + relativeUri; return new Uri(newUri); } } // must throw if Uri is unknown/unhandled throw new ArgumentException(); } here's the code for GetEntity /// /// Gets the entity associated to the given Uri, role, and /// Type. /// /// /// /// The only Type that is supported is the System.IO.Stream.

/// /// /// The only Uri's supported are those for W3C XHTML 1.0. /// /// public override object GetEntity(Uri absoluteUri, string role, Type t) { // only handle streams if (t! = typeof(System.IO.

Stream)) throw new ArgumentException(); if (absoluteUri == null) throw new ArgumentException(); var uri = absoluteUri. AbsoluteUri; foreach (var key in knownDtds. Keys) { if (uri.

StartsWith(knownDtdskey)) { // Return the stream containing the requested DTD. // This can be a FileStream, HttpResponseStream, MemoryStream, // or whatever other stream you like. I used a Resource stream // myself.

If you retrieve the DTDs via HTTP, you could use your // own IWebProxy here. Var resourceName = GetResourceName(key, uri. Substring(knownDtdskey.

Length)); return GetStreamForNamedResource(resourceName); } } throw new ArgumentException(); } The full working code for my custom resolver is available. If your resolver does network comms, then for a general solution you may want to override the Credentials property. Public override System.Net.

ICredentials Credentials { set { ... } } Also, you may want to expose a Proxy property. Or not. As I said above, you may want to automatically determine the proxy to use, from the DTD URI.

Thanks for the Cheeso reply. Can you show some code for MyCustomDtdResolver? Like you say in stackoverflow.com/questions/2558021/… the docs are very scanty.

I'm specifically interested in what the ResolveUri and GetEntity methods should contain to enable fetching the dtd via a proxy. – Lanceomagnifico May 5 '10 at 8:28 Oh, and I did forget to mention that this solution needs to work in . NET 3.5 – Lanceomagnifico May 5 '10 at 8:31 1 ok, I will update the answer with some code.

– Cheeso May 5 '10 at 13:05 Thanks for the additional detail and source code! Very nice commenting with examples. The Conditional attribute looks to be very useful!

– Lanceomagnifico May 5 '10 at 12:03.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions