我试图通过.dtd验证一个xml文件.我写了这个验证器:
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文件,我真的找不到如何.
感谢您的时间.
最佳答案 在Xml文件中声明与DTD的关联:
dtd存储在远程服务器中的示例:
<!DOCTYPE Catalog PUBLIC "abc/Catalog" "http://xyz.abc.org/dtds/catalog.dtd">
查看wiki page和that site,了解有关Xml文件和DTD关联的更多选项和信息.
在本地放置dtd的情况下的示例(SYSTEM):
How to reference a DTD from a document:
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">
Notes:
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.
(摘录自here)