views:

220

answers:

3

Hello! So I'm currently working on an ePub reader application, and I've been reading through a bunch of regular XML files just fine with System.Xml and XmlDocument:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Path.Combine(Directory.GetCurrentDirectory(), "META-INF/container.xml"));
XmlNodeList xnl = xmldoc.GetElementsByTagName("rootfile");

However, now I'm trying to open the XHTML files that contain the actual book text, and they're XHTML files. Now I don't really know the difference between the two, but I'm getting the following error with this code (in the same document, using the same XmlDocument and XmlNodeList variable)

xmldoc.Load(Path.Combine(Directory.GetCurrentDirectory(), "OEBPS/part1.xhtml"));

"WebException was unhandled: The remote server returned an error: (503) Server Unavailable"

It's a local document, so I'm not understanding why it's giving this error? Any help would be greatly appreciated. :)

I've got the full source code here if it helps: http://drop.io/epubtest

(I know the ePubConstructor.ParseDocument() method is horribly messy, I'm just trying to get it working at the moment before I split it into classes)

+2  A: 

Try to remove the DOCTYPE from the XHTML file, probably you have link to an external DTD.

volody
Thanks for the response! That does work, however the purpose of the program will be to take these existing files and read them in, so I won't be able to manually remove the DOCTYPE line from each file. Is there a way I can pull it in, and ignore that tag?
kcoppock
A: 

Try the following untested code:

XmlDocument xmldoc = new XmlDocument(); 
XmlReaderSettings settings = new XmlReaderSettings
{
    XmlResolver = new XmlUrlResolver()
};
using (var reader = XmlReader.Create(
    Path.Combine(Directory.GetCurrentDirectory(), 
                 "OEBPS/part1.xhtml"), settings))
{
    xmlDoc.Load(reader);
}
John Saunders
Thank you! I also found another suggestion from your code, that I can do just `xmldoc.XmlResolver = null;` and this works as well. I am not sure if one is preferable over the other, but your code was very helpful! (plus you gave me the tip of the using (var...) which will help me clean up my code quite a bit)
kcoppock
A: 

Try the following code :

XmlDocument xmldoc = new XmlDocument();

doc.XmlResolver = null; // this ignores the DTD

xmldoc.Load(Path.Combine(Directory.GetCurrentDirectory(), "META-INF/container.xml"));

XmlNodeList xnl = xmldoc.GetElementsByTagName("rootfile");

Sur