本文整理汇总了C#中System.Xml.Schema.XmlSchemaSet类的典型用法代码示例。如果您正苦于以下问题:C# XmlSchemaSet类的具体用法?C# XmlSchemaSet怎么用?C# XmlSchemaSet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlSchemaSet类属于System.Xml.Schema命名空间,在下文中一共展示了XmlSchemaSet类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class Sample
{
public static void Main() {
// Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet();
// Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd");
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += ValidationCallBack;
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e) {
Console.WriteLine($"Validation Error:\n {e.Message}\n");
}
}
输出:
Validation Error: The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in namespace 'urn:bookstore-schema'. Validation Error: The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in namespace 'urn:bookstore-schema'.