XML校正學習之二

來源:互聯網
上載者:User

    在XmlReaderSettings類中,Schemas屬性工作表示為一個集合—— 即XmlSchemaSet類的執行個體,允許將打算用於之後驗證的一個或多個模式儲存起來。使用模式集合可以提高整體效能,因為各種模式都儲存在記憶體中而不需要在每次驗證的時候進行載入。可以按需要添加任意多個XSD模式,但是請記住在第一次調用Read之前,集合必須完成。

為了添加一個新的模式至緩衝,可以使用XmlSchemaSet對象的Add()方法。該方法具有一些重載形式,如下所示:

public void Add(XmlSchemaSet);

public XmlSchema Add(XmlSchema);

public XmlSchema Add(string, string);

public XmlSchema Add(string, XmlReader);

第一個重載將所有定義於給定集合中的模式填充至當前集合中。餘下的3個重載由不同資料建立並返回XmlSchema類的執行個體—— 包含XSD模式定義的.NET Framework類。

1. 填充模式集合

模式集合實際上由XmlSchema類的執行個體組成—— 一種模式編譯後的版本類型。Add方法的不同重載允許以不同的輸入參數建立XmlSchema對象。例如,考慮如下方法:

public XmlSchema Add(string ns, string url);

這個方法建立並添加了一個新的模式對象至集合中。使用與模式相關聯的命名空間URI和源的URL來建立編譯後的模式對象。

可以使用Contains()方法檢查一個模式是否已經在模式集合中。Contains()方法可以接受一個XmlSchema對象,也可以接受一個表示與模式相關聯的命名空間URI的字串。前面的方式只用於XSD模式。而後面的方式則可用於XSD和XDR模式。

2. 使用XmlSchemaSet類驗證XML資料

XmlSchemaSet類表示XML模式的緩衝。它允許您為相同的目標命名空間將多個模式編譯成單個邏輯模式。

注意:

XmlSchemaSet類代替了XmlSchemaCollection類,後者是在.NET Framework 1.x中將模式放入緩衝所選擇的類。新的XmlSchemaSet類不僅提供了更好的標準相容性而且還提高了效能。

在查看樣本之前,先簡要介紹一下XmlSchemaSet類的重要屬性和方法。表5-4提供了XmlSchemaSet類中重要屬性的列表。

表5-4  XmlSchemaSet類的重要屬性

屬    性

說    明

Count

獲得包含在XmlSchemaSet中的邏輯XSD模式數量

GlobalAttributes

獲得對包含在XmlSchemaSet中所有的XSD模式的所有全域屬性的引用

GlobalElements

獲得對包含在XmlSchemaSet中所有的XSD模式的所有全域元素的引用

GlobalTypes

獲得包含在XmlSchemaSet中的所有XSD模式的所有全域簡單和複雜的類型

IsCompiled

指示在XmlSchemaSet中的XSD模式是否已經被編譯

 

表5-5討論了XmlSchemaSet類的重要方法。

表5-5  XmlSchemaSet類的重要方法

方    法

說    明

Add

將給定的XSD模式添加至XmlSchemaSet

Compile

將添加至XmlSchemaSet類的XSD模式編譯成單個邏輯模式,然後用於驗證

Contains

允許您檢查所提供的XSD模式是否在XmlSchemaSet之中

Remove

從XmlSchemaSet中移除指定的XSD模式

Reprocess

重新處理已經存在於XmlSchemaSet之中的XSD模式

 

程式清單5-4中的樣本顯示了如何利用XmlSchemaSet類來驗證XML資料。

程式清單5-4  使用XmlSchemaSet類驗證XML資料

<%@ Page Language="C#"%>

<%@ Import Namespace="System.Xml" %>

<%@ Import Namespace="System.Xml.Schema" %>

<script runat="server">   

  private StringBuilder _builder = new StringBuilder();

  void Page_Load(object sender, EventArgs e)

  {       

    string xmlPath = Request.PhysicalApplicationPath +

      @""App_Data"Authors.xml";   

    string xsdPath = Request.PhysicalApplicationPath +

      @""App_Data"Authors.xsd";

    XmlSchemaSet schemaSet = new XmlSchemaSet();

    schemaSet.Add(null, xsdPath);

    XmlReader reader = null;       

    XmlReaderSettings settings = new XmlReaderSettings();

    settings.ValidationEventHandler += new

      ValidationEventHandler(this.ValidationEventHandler);

    settings.ValidationType = ValidationType.Schema;       

    settings.Schemas = schemaSet;        

    reader = XmlReader.Create(xmlPath, settings);

    while (reader.Read())

    {           

    }

    if (_builder.ToString() == String.Empty)

      Response.Write("Validation completed successfully.");

    else

      Response.Write("Validation Failed. <br>" + _builder.ToString());

  }

 

  void ValidationEventHandler(object sender, ValidationEventArgs args)

  {       

    _builder.Append("Validation error: " + args.Message + "<br>");               

  } 

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

  <title>XSD Validation using XmlSchemaSet</title>

</head>

<body>

  <form id="form1" runat="server">

    <div>

    </div>

  </form>

</body>

</html>

在程式清單5-4中,當建立完XmlSchemaSet類的執行個體之後,就調用Add方法將Authors.xsd模式添加至XmlSchemaSet類。

    XmlSchemaSet schemaSet = new XmlSchemaSet();

    schemaSet.Add(null, xsdPath);

當把這個模式添加至XmlSchemaSet之後,只要將XmlReaderSettings對象的Schemas屬性設定為XmlSchemaSet對象。

    settings.Schemas = schemaSet;    

然後在迴圈中調用XmlReader對象的Read方法來分析XML資料。與前面的樣本類似的是,解析器只在XML資料不是格式良好的時候才停止。因為在出現驗證錯誤時並不停止,所以就可以在一個過程中找到所有的驗證錯誤而無需反覆分析XML文檔。如果使用瀏覽器瀏覽這個頁面,將會看到與圖5-1顯示相同的輸出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.