【问题标题】:Validation of Docbook 5.0 XML via XSD with C# .Net使用 C# .Net 通过 XSD 验证 Docbook 5.0 XML
【发布时间】:2014-09-29 16:29:16
【问题描述】:

我尝试通过 c# 和 xsd 数据验证 docbook 5.0 xml。

我从这里的示例开始:http://msdn.microsoft.com/en-us/library/ckztbtx6(v=vs.110).aspx,它非常简单。 我使 notValidXSD.xml(在示例中)有效,删除了第一本书调用它 nowValidXSD.xml 并且它的文字很好。

现在我尝试了同样的 Docbook 5.0 格式(docbook.xsd、xml.xsd xlink.xml),您可以在此处下载 http://www.docbook.org/xml/5.0/xsd/

尝试代码(由上面 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.

(我必须在 http 之后留出空格,因为这是我的第一个问题) 我不知道该怎么办了。对我来说,这些文件看起来不错。我用谷歌搜索了几个小时的问题,但似乎无法通过 c# 进行验证。

这里是文件:

<!-- 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>

还有文档 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>

【问题讨论】:

    标签: c# xml validation xsd docbook


    【解决方案1】:

    问题在于 XmlValidatingReader 不仅已经过时,而且还没有做应该做的事情。

    我基本上从这里发布我的新验证方法:http://msdn.microsoft.com/en-US/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();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多