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

  • SEARCH
  • Home
  • 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 707153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:14:45+00:00 2026-05-14T04:14:45+00:00

I’m trying to do xpath queries over an xhtml document. Using .NET 3.5. The

  • 0

I’m trying to do xpath queries over an xhtml document. Using .NET 3.5.

The document looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
   ....
  </head>
  <body>
    ...
  </body>
</html>

Because the document includes various char entities (&nbsp; and so on), I need to use the DTD, in order to load it with an XmlReader. So my code looks like this:

var s = File.OpenRead(fileToRead)
var reader = XmlReader.Create(s, new XmlReaderSettings{ ProhibitDtd=false });

But when I run this, it returns

An error has occurred while opening external DTD ‘http://www.w3.org/TR/xhtml1-transitional.dtd‘: The remote server returned an error: (503) Server Unavailable.

Now, I know why I am getting the 503 error. W3C explained it very clearly.

I’ve seen “workarounds” where people just disable the DTD. This is what ProhibitDtd=true can do, and it eliminates the 503 error.

But in my case that leads to other problems – the app doesn’t get the entity defintions and so isn’t well-formed XML. How can I validate with the DTD, and get the entity definitions, without hitting the w3.org website?


I think .NET 4.0 has a nifty built-in capability to handle this situation: the XmlPreloadedResolver. But I need a solution for .NET 3.5.


related:
– java.io.IOException: Server returned HTTP response code: 503

  • 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-14T04:14:45+00:00Added an answer on May 14, 2026 at 4:14 am

    The answer is, I have to provide my own XmlResolver. I don’t think this is built-in to .NET 3.5. That’s baffling. It’s also baffling that it has taken me this long to stumble onto this problem. It’s also baffling that I couldn’t find someone else who solved this problem already?

    Ok, so.. the XmlResolver. I created a new class, derived from XmlResolver and over-rode three key things: Credentials (set), ResolveUri and GetEntity.

    public sealed class XhtmlResolver : XmlResolver
    {
        public override System.Net.ICredentials Credentials
        {
            set { throw new NotSupportedException();}
        }
    
        public override object GetEntity(Uri absoluteUri, string role, Type t)
        {
           ...
        }
    
        public override Uri ResolveUri(Uri baseUri, string relativeUri)
        {
          ...
        }
    }
    

    The documentation on this stuff is pretty skimpy, so I’ll tell you what I learned. The operation of this class is like so: the XmlReader will call ResolveUri first, then, given a resolved Uri, will then call GetEntity. That method is expected to return an object of Type t (passed as a param). I have only seen it request a System.IO.Stream.

    My idea is to embed local copies of the DTD and its dependencies for XHTML1.0 into the assembly, using the csc.exe /resource option, and then retrieve the stream for that resouce.

    private System.IO.Stream GetStreamForNamedResource(string resourceName)
    {
        Assembly a = Assembly.GetExecutingAssembly();
        return  a.GetManifestResourceStream(resourceName);
    }
    

    Pretty simple. This gets called from GetEntity().

    But I can improve on that. Instead of embedding the DTDs in plaintext, I gzipped them first. Then modify the above method like so:

    private System.IO.Stream GetStreamForNamedResource(string resourceName)
    {
        Assembly a = Assembly.GetExecutingAssembly();
        return  new System.IO.Compression.GZipStream(a.GetManifestResourceStream(resourceName), System.IO.Compression.CompressionMode.Decompress);
    }
    

    That code opens the stream for an embedded resource, and returns a GZipStream configured for decompression. The reader gets the plaintext DTD.

    What I wanted to do is resolve only URIs for DTDs from Xhtml 1.0. So I wrote the ResolveUri and GetEntity to look for those specific DTDs, and respond affirmatively only for them.

    For an XHTML document with the DTD statement, the flow is like this;

    1. XmlReader calls ResolveUri with the public URI for the XHTML DTD, which is "-//W3C//DTD XHTML 1.0 Transitional//EN". If the XmlResolver can resolve, it should return… a valid URI. If it cannot resolve, it should throw. My implementation just throws for the public URI.

    2. XmlReader then calls ResolveUri with the System Identifier for the DTD, which in this case is "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd". In this case, the XhtmlResolver returns a valid Uri.

    3. XmlReader then calls GetEntity with that URI. XhtmlResolver grabs the embedded resource stream and returns it.

    The same thing happens for the dependencies – xhtml_lat1.ent, and so on. In order for the resolver to work, all those things need to be embedded.

    And yes, if the Resolver cannot resolve a URI, it is expected to throw an Exception. This isn’t officially documented as far as I could see. It seems a bit surprising. (An egregious violation of the principle of least astonishment). If instead, ResolveUri returns null, the XmlReader will call GetEntity on the null URI, which …. ah, is hopeless.


    This works for me. It should work for anyone who does XML processing on XHTML from .NET. If you want to use this in your own applications, grab the DLL. The zip includes full source code. Licensed under the MS Public License.

    You can plug it into your XML apps that fiddle with XHTML. Use it like this:

    // for an XmlDocument...
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.XmlResolver = new Ionic.Xml.XhtmlResolver();
    doc.Load(xhtmlFile);
    
    // for an XmlReader...
    var xmlReaderSettings = new XmlReaderSettings
        {
            ProhibitDtd = false,
            XmlResolver = new XhtmlResolver()
        };
    using (var stream = File.OpenRead(fileToRead))
    {
        XmlReader reader = XmlReader.Create(stream, xmlReaderSettings);
        while (reader.Read())
        {
         ...
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a command, sp_spaceused that will get you the… May 14, 2026 at 11:34 pm
  • Editorial Team
    Editorial Team added an answer If you have your UITextField as an attribute of the… May 14, 2026 at 11:34 pm
  • Editorial Team
    Editorial Team added an answer solved, my problem is the permission: https://developers.facebook.com/docs/reference/api/permissions/ May 14, 2026 at 11:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.