问题
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