[英]Validation of Docbook 5.0 XML via XSD with C# .Net
I try to validate a docbook 5.0 xml via c# and xsd data. 我尝试通过C#和XSD数据验证docbook 5.0 xml。
I started from the example here: http://msdn.microsoft.com/en-us/library/ckztbtx6(v=vs.110).aspx where it is pretty easy and simple. 我从以下示例开始: http : //msdn.microsoft.com/zh-cn/library/ckztbtx6(v=vs.110).aspx ,该示例非常简单。 I made the notValidXSD.xml (in the example) valid removing the first book calling it nowValidXSD.xml and it words fine. 我使notValidXSD.xml(在此示例中)有效,从而删除了第一本书,即nowValidXSD.xml,它的文字还不错。
Now I tried the same for the Docbook 5.0 format ( docbook.xsd, xml.xsd xlink.xml ) which you can download here http://www.docbook.org/xml/5.0/xsd/ 现在,我尝试了相同的Docbook 5.0格式(docbook.xsd,xml.xsd xlink.xml),您可以在此处下载http://www.docbook.org/xml/5.0/xsd/
Trying the code (given by the example on msdn above) gives me: 尝试代码(通过上面的msdn上的示例给出)可以给我:
Validating XML file mydocxml.xml
Validation error: the http ://www.w3.org/XML/1998/namespace:id-Attribute is not declared.
Validation error: the http ://docbook.org/ns/docbook:article-Element is not declared.
Validation error: the schema informationen für das Attribute 'version' could not be found.
Validation error: the http://docbook.org/ns/docbook:title-Element is not declared.
Validation error: the http://docbook.org/ns/docbook:para-Element is not declared.
Validation finished. Validation failed.
(I had to make spaces after http because its my first question here) I don't know what to do any more. (我必须在http后留空格,因为这是我的第一个问题)我不知道该怎么办。 For me the files look fine. 对我来说,文件看起来不错。 I tried Googling the problem for hours now and can't seem to be able to validate via c#. 我已经尝试了Googling几个小时的问题,但似乎无法通过c#进行验证。
Here are the files: 这些是文件:
<!-- nowValidXSD.xml -->
<?xml version='1.0' encoding="UTF-8"?>
<bookstore xmlns="urn:bookstore-schema"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:bookstore-schema books.xsd">
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
And the docbook xml: 和docbook xml:
<!-- mydocxml.xml -->
<?xml version="1.0" encoding="UTF-8"?> <!-- -->
<article xmlns="http ://docbook.org/ns/docbook" version="5.0"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http ://docbook.org/ns/docbook docbook.xsd">
<title>some title</title>
<para> some text.</para>
</article>
The problem was that XmlValidatingReader is not only obsolete it is also not doing what is is supposed to be doing. 问题是XmlValidatingReader不仅过时了,而且还没有执行应做的事情。
I post my new Validation method basically from here: http://msdn.microsoft.com/en-US/library/as3tta56(v=vs.80).aspx in hopes that is helps someone :) 我基本上从这里发布我的新验证方法: http : //msdn.microsoft.com/zh-CN/library/as3tta56 (v= vs.80).aspx ,希望对别人有所帮助:)
private Boolean m_success = true;
private void ValidationCallBack(object sender, ValidationEventArgs args)
{
m_success = false;
Console.WriteLine("\r\n\tValidation error: " + args.Message);
}
public void validate(string xmlfile, string ns, string xsdfile)
{
m_success = true;
XmlReaderSettings settings = new XmlReaderSettings();
XmlSchemaSet sc = new XmlSchemaSet();
sc.Add(ns, xsdfile);
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
XmlReader reader = XmlReader.Create(xmlfile, settings);
// Parse the file.
while (reader.Read()) ;
Console.WriteLine("Validation finished. Validation {0}", (m_success == true ? "successful!" : "failed."));
reader.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.