内容简介:翻译自:https://stackoverflow.com/questions/15098166/how-can-i-force-compliance-with-a-given-schema-in-net
假设我有一个模式,我希望输入文档符合该模式.我按照这样的架构加载文件:
// Load the ABC XSD
var schemata = new XmlSchemaSet();
string abcSchema = FooResources.AbcTemplate;
using (var reader = new StringReader(abcSchema))
using (var schemaReader = XmlReader.Create(reader))
{
schemata.Add(string.Empty, schemaReader);
}
// Load the ABC file itself
var settings = new XmlReaderSettings
{
CheckCharacters = true,
CloseInput = false,
ConformanceLevel = ConformanceLevel.Document,
IgnoreComments = true,
Schemas = schemata,
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
};
XDocument inputDoc;
try
{
using (var docReader = XmlReader.Create(configurationFile, settings))
{
inputDoc = XDocument.Load(docReader);
}
}
catch (XmlSchemaException xsdViolation)
{
throw new InvalidDataException(".abc file format constraint violated.", xsdViolation);
}
这在检测文件中的琐碎错误时工作正常.但是,由于架构已锁定到命名空间,因此类似以下的文档无效,但可以通过:
<badDoc xmlns="http://Foo/Bar/Bax"> This is not a valid document; but Schema doesn't catch it because of that xmlns in the badDoc element. </badDoc>
我想说只有我有架构的名称空间应该通过架构验证.
看起来很愚蠢,你想看的东西实际上是在XmlReaderSettings对象上:
settings.ValidationEventHandler +=
(node, e) => Console.WriteLine("Bad node: {0}", node);
翻译自:https://stackoverflow.com/questions/15098166/how-can-i-force-compliance-with-a-given-schema-in-net
以上所述就是小编给大家介绍的《c# – 如何在.NET中强制遵从给定的模式?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 任何给定的DOM元素的Javascript字数
- algorithm – 给定exp()函数,如何实现ln()函数?
- python使用筛选法计算小于给定数字的所有素数
- 在MySql中,查找具有给定前缀的字符串
- C语言求给定范围内的所有素数代码及解析
- ruby-on-rails – Rspec 3.6,Rails 5错误:`post`请求的参数数量错误(给定2,预期1)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
数据挖掘十大算法
(美)吴信东(Xindong Wu)、(美),库玛尔 ,(Vipin Kumar) / 李文波、吴素研 / 清华大学出版社 / 2013-5 / 39.00元
《世界著名计算机教材精选:数据挖掘十大算法》详细介绍了在实际中用途最广、影响最大的十种数据挖掘算法,这十种算法是数据挖掘领域的顶级专家进行投票筛选的,覆盖了分类、聚类、统计学习、关联分析和链接分析等重要的数据挖掘研究和发展主题。《世界著名计算机教材精选:数据挖掘十大算法》对每一种算法都进行了多个角度的深入剖析,包括算法历史、算法过程、算法特性、软件实现、前沿发展等,此外,在每章最后还给出了丰富的习......一起来看看 《数据挖掘十大算法》 这本书的介绍吧!