Need help grasping RelaxngValidatingReader in Commons.Xml.Relaxng

北城以北 提交于 2019-12-11 05:28:19

问题


Environment: RelaxngValidatingReader .Net 2.0 Framework

I can't seem to get the validator to equal false, no matter how much I change the xml around. Am I missing some concept here?

Resource
https://github.com/mono/mono/tree/master/mcs/class/Commons.Xml.Relaxng

rnc compact format

start = root
root = element root { parent1* & parent2* }
parent1 = element parent1 { element field { attribute usecase_123 {text} } }
parent2 = element parent2 { element field { attribute usecase_AAA {text} } }

xml instance

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <parent1>
    <hello/>
  </parent1>
</root>

Main Program

static void Main(string[] args)
{
    RncParser parser = new RncParser(new NameTable());
    TextReader source = new StreamReader("test.rnc");
    RelaxngPattern grammar = parser.Parse(source);

    XmlReader instance = new XmlTextReader("XMLFile1.xml");
    RelaxngValidatingReader reader = new RelaxngValidatingReader(instance, grammar);
    Console.WriteLine("Was the xml valid? {0}", reader.Read());
}

回答1:


You have to repeat Read() until End-of-file:

static void Main(string[] args)
{
    RncParser parser = new RncParser(new NameTable());
    TextReader source = new StreamReader("test.rnc");
    RelaxngPattern grammar = parser.Parse(source);

    XmlReader instance = new XmlTextReader("XMLFile1.xml");
    RelaxngValidatingReader reader = new RelaxngValidatingReader(instance, grammar);
    try {
        while (!reader.EOF) {
            reader.Read();
        }
        Console.WriteLine("validation succeeded");
    }
    catch (Exception ex) {
        Console.WriteLine("validation failed with message:");
        Console.WriteLine(ex.Message);
    }
}


来源:https://stackoverflow.com/questions/44853309/need-help-grasping-relaxngvalidatingreader-in-commons-xml-relaxng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!