此代码未正确验证 Xml 您能否找到错误.... 即使我使用 Invalid xml 执行它也不会引发任何错误
using System.Xml;
namespace XmlTryProject
{
internal class Program
{
private static void Main(string[] args)
{
XmlReaderSettings readSettings = new XmlReaderSettings();
readSettings.ValidationType = ValidationType.Schema;
readSettings.Schemas.Add(null,
@"C:\Visual Studio 2010\Projects\XmlTry \XmlTryProject\EmployeeXSD.xsd");
readSettings.ValidationEventHandler +=
new System.Xml.Schema.ValidationEventHandler(Validater);
XmlReader xReader = XmlReader.Create(
@"C:\Visual Studio 2010\Projects\XmlTry\XmlTryProject\EmployeeXML.xml",
readSettings);
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
Console.WriteLine(xReader.Name);
}
}
}
public static void Validater(object sender,
System.Xml.Schema.ValidationEventArgs args)
{
Console.WriteLine(args.Message);
}
}
}