本文整理汇总了C#中System.Xml.Schema.XmlSchemaCollection.Add方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSchemaCollection.Add方法的具体用法?C# XmlSchemaCollection.Add怎么用?C# XmlSchemaCollection.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Schema.XmlSchemaCollection
的用法示例。
在下文中一共展示了XmlSchemaCollection.Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SchemaCollectionSample
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public class SchemaCollectionSample
{
private const String doc1 = "booksSchema.xml";
private const String doc2 = "booksSchemaFail.xml";
private const String doc3 = "newbooks.xml";
private const String schema = "books.xsd";
private const String schema1 = "schema1.xdr";
private XmlTextReader reader=null;
private XmlValidatingReader vreader = null;
private Boolean m_success = true;
public SchemaCollectionSample ()
{
//Load the schema collection.
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add("urn:bookstore-schema", schema); //XSD schema
xsc.Add("urn:newbooks-schema", schema1); //XDR schema
//Validate the files using schemas stored in the collection.
Validate(doc1, xsc); //Should pass.
Validate(doc2, xsc); //Should fail.
Validate(doc3, xsc); //Should fail.
}
public static void Main ()
{
SchemaCollectionSample validation = new SchemaCollectionSample();
}
private void Validate(String filename, XmlSchemaCollection xsc)
{
m_success = true;
Console.WriteLine();
Console.WriteLine("Validating XML file {0}...", filename.ToString());
reader = new XmlTextReader (filename);
//Create a validating reader.
vreader = new XmlValidatingReader (reader);
//Validate using the schemas stored in the schema collection.
vreader.Schemas.Add(xsc);
//Set the validation event handler
vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
//Read and validate the XML data.
while (vreader.Read()){}
Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful" : "failed"));
Console.WriteLine();
//Close the reader.
vreader.Close();
}
private void ValidationCallBack (object sender, ValidationEventArgs args)
{
m_success = false;
Console.Write("\r\n\tValidation error: " + args.Message);
}
}
示例2: XmlSchemaCollection
XmlSchemaCollection sc = new XmlSchemaCollection();
sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create a resolver with the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Add the new schema to the collection.
sc.Add("", new XmlTextReader("sample.xsd"), resolver);