当前位置: 首页>>代码示例>>C#>>正文


C# XmlValidatingReader.Schemas属性代码示例

本文整理汇总了C#中System.Xml.XmlValidatingReader.Schemas属性的典型用法代码示例。如果您正苦于以下问题:C# XmlValidatingReader.Schemas属性的具体用法?C# XmlValidatingReader.Schemas怎么用?C# XmlValidatingReader.Schemas使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。


在下文中一共展示了XmlValidatingReader.Schemas属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
  }  
}
开发者ID:.NET开发者,项目名称:System.Xml,代码行数:68,代码来源:XmlValidatingReader.Schemas


注:本文中的System.Xml.XmlValidatingReader.Schemas属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。