问题描述
我正在尝试通过.dtd验证xml文件。我已经写了这个验证器:
I am trying to validate an xml file via a .dtd. I have write this validator:
public bool Validation(XmlDocument xmlDoc)
{
var xml = XmldocToString(xmlDoc);
var r = new XmlTextReader(new StringReader(xml));
var settings = new XmlReaderSettings();
var sb = new StringBuilder();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += (a, e) =>
{
sb.AppendLine(e.Message);
_isValid = false;
};
XmlReader validator = XmlReader.Create(r, settings);
while (validator.Read())
{
}
validator.Close();
return _isValid;
}
问题是我必须在解决方案的bin目录中拥有dtd文件。我想选择一个不同的目录来保存.dtd文件,但我真的找不到方法。
The problem is that I must have the dtd file in bin directory of the Solution. I want to chose a diferent directory to keep the .dtd file and i really can't find how.
谢谢您的时间。
推荐答案
在Xml文件中声明与DTD的关联:
Declare in the Xml file the association with the DTD:
<!DOCTYPE Catalog PUBLIC "abc/Catalog" "http://xyz.abc.org/dtds/catalog.dtd">
看看那个,并位于以获取有关Xml文件和DTD关联的更多选项和信息。
Take a look at that wiki page and at that site for more options and information about Xml files and DTD association.
假定文档的顶部元素为规范,并且将dtd放置在在文件从
加载的目录的子目录dtds中添加文件mydtd :
Assuming the top element of the document is spec and the dtd is placed in the file mydtd in the subdirectory dtds of the directory from where the document were loaded:
<!DOCTYPE spec SYSTEM "dtds/mydtd">
系统字符串实际上是URI参考(如RFC
2396中所定义),因此您可以使用完整的URL字符串来指示
DTD在服务器上的位置。网络。如果您希望其他人
验证您的文档,这确实是一件好事。也可以关联PUBLIC
标识符(魔术字符串),以便在客户端的目录
中查找DTD,而不必在网络上定位它。 DTD
包含一组元素和属性声明,但
并未定义文档的根目录。
明确地作为DOCTYPE
声明的第一个元素告知解析器/验证器。
The system string is actually an URI-Reference (as defined in RFC 2396) so you can use a full URL string indicating the location of your DTD on the Web. This is a really good thing to do if you want others to validate your document. It is also possible to associate a PUBLIC identifier (a magic string) so that the DTD is looked up in catalogs on the client side without having to locate it on the web. A DTD contains a set of element and attribute declarations, but they don't define what the root of the document should be. This is explicitly told to the parser/validator as the first element of the DOCTYPE declaration.
(摘录自)
这篇关于通过dtd验证xml,这是dtd的其他目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!