如何使用自定义XmlUrlResolver将XHTML文件加载到XElement中? [英] How to load an XHTML file into an XElement using a custom XmlUrlResolver?

查看:173
本文介绍了如何使用自定义XmlUrlResolver将XHTML文件加载到XElement中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得加载到LINQ的XElement的XHTML文件。不过,我正在与解析问题。这个问题有如下定义做:

I am trying to get an XHTML file loaded into an LINQ XElement. However, I am running into problems with the resolver. The problem has to do with the following definition:

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



我有一个重写的GetEntity它转换环节,如
自定义XmlUrlResolver的 http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 DTD ,以本地资源流。这几乎整个DTD XHTML工作正常。唯一一个我不能真正解决是URI - // W3C // DTD XHTML 1.0过渡// EN,我不知道我应该用它做的事情。

I have a custom XmlUrlResolver with an overridden GetEntity which converts links such as http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd to a local resource stream. This works fine for almost the entire XHTML DTD. The only one I am unable to actually resolve is the Uri "-//W3C//DTD XHTML 1.0 Transitional//EN" and I am not sure what I should be doing with it.

    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        var resourceName = "ePub.DTD." + absoluteUri.Segments[absoluteUri.Segments.GetLength(0) - 1];
        if (_resources.Contains(resourceName))
        {
            Stream dataStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
            return dataStream;
        }
        return base.GetEntity(absoluteUri, role, ofObjectToReturn);
    }



正如你在上面的代码中看到的,什么我解决不了被处理默认的 XmlUrlResolver 。这意味着,首先是上面的链接 - // W3C /。然而,基础方法抛出 DirectoryNotFoundException 但是。继续将加载的XElement 就好了。如果我不是返回一个空流它会导致XHTML的装载到的XElement 期间抛出一个错误。

As you see in the above code, anything I cannot resolve is handled by the default XmlUrlResolver. This means the above link starting with -//W3C/. The base method however throws an DirectoryNotFoundException however. Continuing will load the XElement just fine. If I instead return an empty stream it causes an error to be throw during loading of the XHTML into the XElement.

任何线索有人可能对处理这样的公共定义具有自定义 XmlUrlResolver

Any clues someone might have about handling such a PUBLIC definition with a custom XmlUrlResolver?

推荐答案

答微软板被盗,地方:

这行为是设计使然。 ( - // W3C // DTD XHTML 1.1 // EN)在DOCTYPE声明同时指定公共ID和系统ID,如果XmlResolver.GetEntity理解公共标识符中的XmlReader首先尝试。因此,它与公众的ID调用GetEntity如果解析器不明白它(如XmlUrlResolver),它抛出一个异常。 XMLReader可以捕获该异常并调用GetEntity,但这次与系统标识符(http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd)。

This behavior is by design. When both the public ID and system ID are specified in the DOCTYPE declaration, the XmlReader first tries if the XmlResolver.GetEntity understands the public identifier ("-//W3C//DTD XHTML 1.1//EN"). So it calls GetEntity with the public ID and if the resolver does not understand it (like the XmlUrlResolver), it throws an exception. The XmlReader catches the exception and calls the GetEntity, but this time with the system identifier ("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd").

谢谢,
-Helena Kotas,开发的System.Xml

Thanks, -Helena Kotas, System.Xml Developer

Gepost门微软运2006年10月5日17:34 OM

Gepost door Microsoft op 10-5-2006 om 17:34

这篇关于如何使用自定义XmlUrlResolver将XHTML文件加载到XElement中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆