我自写xml的schema如何引入啊?

发布时间:2024-05-10 12:45 发布:上海旅游网

问题描述:

哪位兄弟有时间举个小例子吧

问题解答:

using System;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaSetExample
{
□□□static void Main()
□□□{
□□□□□□□XmlReaderSettings booksSettings = new XmlReaderSettings();
□□□□□□□booksSettings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
□□□□□□□booksSettings.ValidationType = ValidationType.Schema;
□□□□□□□booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);

□□□□□□□XmlReader books = XmlReader.Create("contosoBooks.xml", booksSettings);

□□□□□□□while (books.Read()) { }
□□□}

□□□static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
□□□{
□□□□□□□if (e.Severity == XmlSeverityType.Warning)
□□□□□□□{
□□□□□□□□□□□Console.Write("WARNING: ");
□□□□□□□□□□□Console.WriteLine(e.Message);
□□□□□□□}
□□□□□□□else if (e.Severity == XmlSeverityType.Error)
□□□□□□□{
□□□□□□□□□□□Console.Write("ERROR: ");
□□□□□□□□□□□Console.WriteLine(e.Message);
□□□□□□□}
□□□}
}

热点新闻