XmlValidatingReader DTD验证 [英] XmlValidatingReader DTD validation

查看:70
本文介绍了XmlValidatingReader DTD验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

我有一个DTD的本地副本,我需要验证传入的XML

文件。

XML文档具有<!DOCTYPE myname SYSTEM" myfile.dtd">定义。

执行以下代码时,XML将通过

XMLResolver解析,并根据本地存储的DTD

文件进行正确验证。

当传入的XML不包含DOCTYPE属性时,会出现问题。

解析器代码永远不会被调用,验证根本不会发生。

我在这里做错了什么?这是一个错误,即使明确的XML文件没有指定DTD文件,也无法强制执行DTD?


XmlDocument doc = new XmlDocument();

XmlValidatingReader reader = new XmlValidatingReader(new

XmlTextReader(stream));


reader.XmlResolver = new MyDTDResolver();


reader.ValidationType = ValidationType.DTD;


doc.Load(读者);


XML文档的流包含在流变量中


这是MyDTDResolver声明:


私有类MyDTDResolver :XmlUrlResolver

{


公共覆盖对象GetEntity(Uri absoluteUri,字符串角色,类型

ofObjectToReturn)


{


return(返回DTD文档的本地副本的流,我/ b $ b验证XML对)


}


}

I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?

XmlDocument doc = new XmlDocument();
XmlValidatingReader reader = new XmlValidatingReader(new
XmlTextReader(stream));

reader.XmlResolver = new MyDTDResolver();

reader.ValidationType = ValidationType.DTD;

doc.Load(reader);

The XML document''s stream is contained in the stream variable

Here is MyDTDResolver declaration:

private class MyDTDResolver:XmlUrlResolver

{

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn)

{

return (returns a stream of the local copy of the DTD document that I
validate XML against)

}

}

推荐答案




Vlad写道:


Vlad wrote:
我有以下代码:
我有DTD的本地副本,我需要验证传入的XML
文档。
XML文档具有<!DOCTYPE myname SYSTEM" myfile.dtd">定义。
执行以下代码时,XML将通过
XMLResolver解析,并根据本地存储的DTD
文件进行正确验证。
当传入的XML包含时出现问题没有DOCTYPE属性。
解析器代码永远不会被调用,验证根本不会发生。
我在这里做错了什么?这是一个错误,即使明确的XML文件没有指定DTD文件也无法强制执行DTD吗?
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?




这是一个已知的整个DTD方法的缺点,解析器确实看起来是一个DOCTYPE声明来验证XML的
。我没有详细查看.NET类来执行DTD验证,但是它可能是你需要通过
运行传入的XML流的

过滤镜像所有节点,但在

处插入DOCTYPE声明开始能够执行验证DTD。

-


Martin Honnen
http://JavaScript.FAQTs.com/



It is a known shortcoming of the whole DTD approach that parsers do look
for a DOCTYPE declaration to validate the XML against. I have not
exhaustively looked at the .NET classes to perform DTD validation but it
might well be that you need to run your incoming XML stream through a
filter mirroring all nodes but inserting the DOCTYPE declaration at the
beginning to be able to perform the validation ágainst the DTD.
--

Martin Honnen
http://JavaScript.FAQTs.com/





Vlad写道:


Vlad wrote:
我有以下代码:
我有一个DTD的本地副本,我需要验证传入的XML
文档。
XML文档有<!DOCTYPE myname SYSTEM" myfile .dtd">定义。
执行以下代码时,XML将通过
XMLResolver解析,并根据本地存储的DTD
文件进行正确验证。
当传入的XML包含时出现问题没有DOCTYPE属性。
解析器代码永远不会被调用,验证根本不会发生。
我在这里做错了什么?这是一个错误,即使明确的XML文件没有指定DTD文件也无法强制执行DTD吗?
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?




这是一个已知的整个DTD方法的缺点,解析器确实看起来是一个DOCTYPE声明来验证XML的
。我没有详细查看.NET类来执行DTD验证,但是它可能是你需要通过
运行传入的XML流的

过滤镜像所有节点,但在

处插入DOCTYPE声明开始能够执行验证DTD。

-


Martin Honnen
http://JavaScript.FAQTs.com/



It is a known shortcoming of the whole DTD approach that parsers do look
for a DOCTYPE declaration to validate the XML against. I have not
exhaustively looked at the .NET classes to perform DTD validation but it
might well be that you need to run your incoming XML stream through a
filter mirroring all nodes but inserting the DOCTYPE declaration at the
beginning to be able to perform the validation ágainst the DTD.
--

Martin Honnen
http://JavaScript.FAQTs.com/


您的文档必须具有DOCTYPE才能执行DTD验证。 XmlResolver是用来解析外部实体的
当然包括外部DTD。

这里没有错误。

Yan


" Vlad" < RE *************** @ comcast.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Your document has to have DOCTYPE to perform DTD validation. XmlResolver is
being used to resolve external entities, including external DTD of course.
There is not bug here.
Yan

"Vlad" <RE***************@comcast.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
我有以下代码:
我有一个DTD的本地副本,我需要验证传入的XML
文档。
XML文档有<!DOCTYPE myname SYSTEM" myfile.dtd" ;>定义。
执行以下代码时,XML将通过
XMLResolver解析,并根据本地存储的DTD
文件进行正确验证。
当传入的XML包含时出现问题没有DOCTYPE属性。

解析器代码永远不会被调用,验证根本不会发生。
我在这里做错了什么?这是一个错误,即使明确的XML文件没有指定DTD文件也无法强制执行DTD吗?

XmlDocument doc = new XmlDocument();
XmlValidatingReader reader = new XmlValidatingReader(new
XmlTextReader(stream));

reader.XmlResolver = new MyDTDResolver();

reader.ValidationType = ValidationType.DTD;

doc.Load(读者);

XML文档的流包含在流变量中

这是MyDTDResolver声明:<私有类MyDTDResolver:XmlUrlResolver

公共覆盖对象GetEntity(Uri absoluteUri,字符串角色,类型
ofObjectToReturn)

返回(返回我验证XML的DTD文档的本地副本流)

}

}
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute.
The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?

XmlDocument doc = new XmlDocument();
XmlValidatingReader reader = new XmlValidatingReader(new
XmlTextReader(stream));

reader.XmlResolver = new MyDTDResolver();

reader.ValidationType = ValidationType.DTD;

doc.Load(reader);

The XML document''s stream is contained in the stream variable

Here is MyDTDResolver declaration:

private class MyDTDResolver:XmlUrlResolver

{

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn)

{

return (returns a stream of the local copy of the DTD document that I
validate XML against)

}

}



这篇关于XmlValidatingReader DTD验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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