DOCTYPE解决方案和ASP.Net的问题

时间:2022-11-12 00:25:43

In a previous question I mentioned some work with a 3rd party DLL whose interface uses a series of XML inputs that are defined using DTDs. Everything has gone smoothly so far, but I still have this nagging issue with resolving the Document Type Declaration in the generated input values.

在上一个问题中,我提到了一些使用第三方DLL的工作,其接口使用一系列使用DTD定义的XML输入。到目前为止,一切都进展顺利,但在生成的输入值中解析文档类型声明时仍然存在这个棘手的问题。

What I can't figure out is what the deciding factor is in determining where to look for the referenced DTD file. If I have a declaration that looks like this:

我无法弄清楚决定在哪里寻找引用的DTD文件的决定因素。如果我有一个如下所示的声明:

<!DOCTYPE ElementName SYSTEM "ElementName.dtd">

My initial thinking was that the application's current execution path is where a parser would look for the DTD. However, when I attempt to use an XML control in ASP.Net, the error I get baffles me...

我最初的想法是,应用程序的当前执行路径是解析器查找DTD的位置。但是,当我尝试在ASP.Net中使用XML控件时,我得到的错误令我感到困惑......

Could not find file 'c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ElementName.dtd'

找不到文件'c:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ ElementName.dtd'

Why is it looking for the DTD there?

为什么要在那里寻找DTD?

Are there any XML gurus out there who can help me out on this one. I really don't have any control over the XML that is returned from this DLL so what am I supposed to do. Is there a way to "register" a DTD with the operating system? Like the GAC?

是否有任何XML专家可以帮助我解决这个问题。我真的无法控制从这个DLL返回的XML,所以我该怎么做。有没有办法在操作系统中“注册”DTD?像GAC一样?

2 个解决方案

#1


Unfortunately the library that generated the XML used a relative url for the dtd rather than a fully qualified one. As such the XmlControl's XmlDocument is using an XmlResolver class to convert the relative path to a fully qualified one. By default it uses an XmlUrlResolver (that is a concrete XmlResolver). This will try to map the location of the dtd to a location it thinks is relative to the Xml document. Trouble is, where is the XmlDocument? Probably in memory which is not relative to anything and the XmlUrlResolver is using the process location instead which in your case is Visual Studio which is located at 'c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe'.

不幸的是,生成XML的库使用了dtd的相对URL而不是完全合格的URL。因此,XmlControl的XmlDocument使用XmlResolver类将相对路径转换为完全限定的路径。默认情况下,它使用XmlUrlResolver(即具体的X​​mlResolver)。这将尝试将dtd的位置映射到它认为相对于Xml文档的位置。麻烦的是,XmlDocument在哪里?可能在内存中与任何内容都不相关,而XmlUrlResolver正在使用进程位置,而在你的情况下是Visual Studio,它位于'c:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ devenv.exe'。

So what can you do? Well I suppose you have to crate you own XmlResolver that inherits from XmlUrlResolver an overrides the ResolveUri method and does something appropriate. Having done that you will have to:

所以,你可以做什么?好吧,我想你必须创建自己的XmlResolver,它继承自XmlUrlResolver并覆盖ResolveUri方法并做一些合适的事情。完成后你必须:

  1. Create an XmlReaderSettings class and set the XmlReolver property to the class you just created.
  2. 创建一个XmlReaderSettings类,并将XmlReolver属性设置为刚刚创建的类。

  3. Create an XmlReader using XmlReader.Create() passing in your document and the XmlSettings object.
  4. 使用传入文档和XmlSettings对象的XmlReader.Create()创建一个XmlReader。

  5. Create an XmlDocument and call Load passing in the XmlReader and finally.
  6. 创建一个XmlDocument并最后调用Load传入XmlReader。

  7. Set the XmlDocument property of your XmlControl to the XmlDocument.
  8. 将XmlControl的XmlDocument属性设置为XmlDocument。

Frankly that is all a bit of a pain, so if it where me I would just use string.Replace to remove the DTD declaration from the document before processing it into XML.

坦率地说,这有点痛苦,所以如果我在哪里,我会使用string.Replace从文档中删除DTD声明,然后再将其处理成XML。

If you are feeling really brave you can create a resolver that inherits directly from XmlResolver. Once you have done that you can override the GetEntity method and then you can get the dtd document from wherever you like. I wrote one once that got dtds from files embedded as resource files, but unfortunately, I don't have the code any more :-(

如果你真的很勇敢,你可以创建一个直接从XmlResolver继承的解析器。完成后,您可以覆盖GetEntity方法,然后您可以从任何地方获取dtd文档。我写过一次从嵌入资源文件的文件中获取dtds,但不幸的是,我没有代码了:-(

#2


If you do not actually care about validating each and every document against its DTD, you could set the XmlResolver property to null on your XmlTextReader (or XmlDocument) to ignore the DTD altogether.

如果您实际上并不关心根据其DTD验证每个文档,可以在XmlTextReader(或XmlDocument)上将XmlResolver属性设置为null以完全忽略DTD。

#1


Unfortunately the library that generated the XML used a relative url for the dtd rather than a fully qualified one. As such the XmlControl's XmlDocument is using an XmlResolver class to convert the relative path to a fully qualified one. By default it uses an XmlUrlResolver (that is a concrete XmlResolver). This will try to map the location of the dtd to a location it thinks is relative to the Xml document. Trouble is, where is the XmlDocument? Probably in memory which is not relative to anything and the XmlUrlResolver is using the process location instead which in your case is Visual Studio which is located at 'c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe'.

不幸的是,生成XML的库使用了dtd的相对URL而不是完全合格的URL。因此,XmlControl的XmlDocument使用XmlResolver类将相对路径转换为完全限定的路径。默认情况下,它使用XmlUrlResolver(即具体的X​​mlResolver)。这将尝试将dtd的位置映射到它认为相对于Xml文档的位置。麻烦的是,XmlDocument在哪里?可能在内存中与任何内容都不相关,而XmlUrlResolver正在使用进程位置,而在你的情况下是Visual Studio,它位于'c:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ devenv.exe'。

So what can you do? Well I suppose you have to crate you own XmlResolver that inherits from XmlUrlResolver an overrides the ResolveUri method and does something appropriate. Having done that you will have to:

所以,你可以做什么?好吧,我想你必须创建自己的XmlResolver,它继承自XmlUrlResolver并覆盖ResolveUri方法并做一些合适的事情。完成后你必须:

  1. Create an XmlReaderSettings class and set the XmlReolver property to the class you just created.
  2. 创建一个XmlReaderSettings类,并将XmlReolver属性设置为刚刚创建的类。

  3. Create an XmlReader using XmlReader.Create() passing in your document and the XmlSettings object.
  4. 使用传入文档和XmlSettings对象的XmlReader.Create()创建一个XmlReader。

  5. Create an XmlDocument and call Load passing in the XmlReader and finally.
  6. 创建一个XmlDocument并最后调用Load传入XmlReader。

  7. Set the XmlDocument property of your XmlControl to the XmlDocument.
  8. 将XmlControl的XmlDocument属性设置为XmlDocument。

Frankly that is all a bit of a pain, so if it where me I would just use string.Replace to remove the DTD declaration from the document before processing it into XML.

坦率地说,这有点痛苦,所以如果我在哪里,我会使用string.Replace从文档中删除DTD声明,然后再将其处理成XML。

If you are feeling really brave you can create a resolver that inherits directly from XmlResolver. Once you have done that you can override the GetEntity method and then you can get the dtd document from wherever you like. I wrote one once that got dtds from files embedded as resource files, but unfortunately, I don't have the code any more :-(

如果你真的很勇敢,你可以创建一个直接从XmlResolver继承的解析器。完成后,您可以覆盖GetEntity方法,然后您可以从任何地方获取dtd文档。我写过一次从嵌入资源文件的文件中获取dtds,但不幸的是,我没有代码了:-(

#2


If you do not actually care about validating each and every document against its DTD, you could set the XmlResolver property to null on your XmlTextReader (or XmlDocument) to ignore the DTD altogether.

如果您实际上并不关心根据其DTD验证每个文档,可以在XmlTextReader(或XmlDocument)上将XmlResolver属性设置为null以完全忽略DTD。