當前位置: 首頁>>代碼示例>>C#>>正文


C# XmlValidatingReader構造函數代碼示例

本文整理匯總了C#中System.Xml.XmlValidatingReader.XmlValidatingReader構造函數的典型用法代碼示例。如果您正苦於以下問題:C# XmlValidatingReader構造函數的具體用法?C# XmlValidatingReader怎麽用?C# XmlValidatingReader使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。


在下文中一共展示了XmlValidatingReader構造函數的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Sample

//引入命名空間
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Sample
{

  private Boolean m_success = true;

  public Sample ()
  {
      //Validate the document using an external XSD schema.  Validation should fail.
      Validate("notValidXSD.xml"); 

      //Validate the document using an inline XSD. Validation should succeed.
      Validate("inlineXSD.xml");
  }    

  public static void Main ()
  {
      Sample validation = new Sample();
  }

  private void Validate(String filename)
  {    
      m_success = true;
      Console.WriteLine("\r\n******");
      Console.WriteLine("Validating XML file " + filename.ToString());
      XmlTextReader txtreader = new XmlTextReader (filename);
      XmlValidatingReader reader = new XmlValidatingReader (txtreader);

      // Set the validation event handler
      reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

      // Read XML data
      while (reader.Read()){}
      Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful!" : "failed."));

      //Close the reader.
      reader.Close();
  }

  //Display the validation error.
  private void ValidationCallBack (object sender, ValidationEventArgs args)
  {
     m_success = false;
     Console.WriteLine("\r\n\tValidation error: " + args.Message );
  }
}
開發者ID:.NET開發者,項目名稱:System.Xml,代碼行數:51,代碼來源:XmlValidatingReader

示例2: Main

//引入命名空間
using System;
using System.IO;
using System.Xml;

public class Sample
{
    public static void Main()
    {
        XmlTextReader reader = null;

        try
        {
            //Create the XML fragment to be parsed.
            string xmlFrag = "<book> " +
                            "<title>Pride And Prejudice</title>" +
                            "<bk:genre>novel</bk:genre>" +
                            "</book>";

            //Create the XmlNamespaceManager that is used to
            //look up namespace information.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace("bk", "urn:sample");

            //Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

            //Implement the reader. 
            reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

            //Parse the XML fragment.  If they exist, display the   
            //prefix and namespace URI of each element.
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    if (string.IsNullOrEmpty(reader.Prefix))
                    {
                        Console.WriteLine("<{0}>", reader.LocalName);
                    }
                    else
                    {
                        Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
                        Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);
                    }
                }
            }
        }

        finally
        {
            if (reader != null)
                reader.Close();
        }
    }
} // End class
開發者ID:.NET開發者,項目名稱:System.Xml,代碼行數:57,代碼來源:XmlValidatingReader


注:本文中的System.Xml.XmlValidatingReader.XmlValidatingReader構造函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。