Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 778725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:48:42+00:00 2026-05-14T19:48:42+00:00

Someone else has already asked a somewhat similar question: Validate an Xml file against

  • 0

Someone else has already asked a somewhat similar question: Validate an Xml file against a DTD with a proxy. C# 2.0

Here’s my problem: We have a website application that needs to use both internal and external resources.

  1. 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.
  2. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:48:42+00:00Added an answer on May 14, 2026 at 7:48 pm

    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.

    /// <summary>
    ///   Resolves URIs.
    /// </summary>
    /// <remarks>
    ///   <para>
    ///     The only Uri's supported are those for W3C XHTML 1.0.
    ///   </para>
    /// </remarks>
    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 = knownDtds[key];
            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

    /// <summary>
    ///   Gets the entity associated to the given Uri, role, and
    ///   Type.
    /// </summary>
    /// <remarks>
    ///   <para>
    ///     The only Type that is supported is the System.IO.Stream.
    ///   </para>
    ///   <para>
    ///     The only Uri's supported are those for W3C XHTML 1.0.
    ///   </para>
    /// </remarks>
    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(knownDtds[key]))
            {
                // 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(knownDtds[key].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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping someone else has already encountered this and has a solution... I'm trying to
A table exists that someone else loaded. I need to query against the table,
This question to which I already found the answer is posted here in case
When viewing someone else's webpage containing an applet, how can I force Internet Explorer
So I'm amending someone else's code, and they've used a data-bound Accordion control. I
I am working on someone else's PHP code and seeing this pattern over and
I'm debugging someone else's code for a web page that is made with ASP.NET
I recall reviewing someone else's PHP code once and he had a function or
I'm trying to understand someone else's Perl code without knowing much Perl myself. I
I'm currently working on someone else's database where the primary keys are generated via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.