gpt4 book ai didi

c# - 通过 dtd 验证 xml,dtd 的不同目录

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:10 28 4
gpt4 key购买 nike

我正在尝试通过 .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 pagethat 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)

关于c# - 通过 dtd 验证 xml,dtd 的不同目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13303672/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com