Versuche ne XML gegen ein XSD zu validieren, der Validator rührt sich allerdings nicht, obwohl ich falsche Elemente baue:
... {
XmlDocument doc = new XmlDocument();
doc.Schemas.Add(null, xsdFile);
ValidationEventHandler v = new ValidationEventHandler(ValidationEventHandler);
doc.AppendChild(doc.CreateElement("hokljk2"));
// nix
doc.Validate(null);
// nix
doc.Validate(v);
doc.Save(Console.Out);
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
throw new ArgumentException("Validierungs Fehler: " + e.Message);
}
Die XSD scheint in doc.Schemas drin zu sein, aber er wirft mir bei leibe keine Exception. Und das Element ist definitiv nicht in der XSD 🙂
Edit: Hier ein Copy Paste Code, der nicht funzen dürfte:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Reflection;
using System.Xml.Schema;
using System.Xml;
static void Main(string[] args)
{
ValidationEventHandler v = new ValidationEventHandler(ValidationEventHandler);
StringReader sr = new StringReader("<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"xy\" xmlns=\"xy\"><xsd:element name=\"cat\" /></xsd:schema>");
XmlSchema s = XmlSchema.Read(sr,v);
XmlDocument doc = new XmlDocument();
string xml = "<cats />";
doc.Load(new StringReader(xml));
doc.Schemas.Add(s);;
doc.Schemas.Compile();
doc.Validate(null);
doc.Validate(v);
// Keine Exception...
return;
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
throw new ArgumentException("Validierungs Fehler: " + e.Message);
}
Life is a short