共用方式為


使用 XmlValidatingReader 解析實體

EntityHandling 屬性定義 XmlValidatingReader 處理實體的方式。

ExpandEntities

如果將 EntityHandling 屬性設定為 ExpandEntitiesXmlValidatingReader 會展開 XML 文件中所有實體。由於展開實體內容,因此 EntityReferenceXmlNodeType 不會出現。這是預設值。

ExpandCharEntities

如果將 EntityHandling 屬性設定為 ExpandCharEntitiesXmlValidatingReader 會展開字元實體,並將一般實體當作 EntityReference 節點型別傳回 (NodeType = XmlNodeType.EntityReference, Name = name of the entity, HasValue = false)。

ResolveEntity

ResolveEntity 方法可用來展開 EntityReference 節點的一般實體,允許您只在需要時展開實體以最佳化實體處理。如果呼叫 GetAttribute 方法,則會展開一般實體。

您可在讀取處理序期間隨時變更實體處理。EntityHandling 設定的變更會在下一次呼叫 Read 方法時生效。

下列程式碼範例會使用 book1.xml 中的 XML 輸入建立 XmlValidatingReader

Imports System
Imports System.IO
Imports System.Xml
 
Public Class Sample
   
   Public Shared Sub Main()
      Dim reader As XmlValidatingReader = Nothing
      Dim txtreader As XmlTextReader = Nothing
      
      Try
         ' Create and load the XmlTextReader with the XML file.
         txtreader = New XmlTextReader("book1.xml")
         txtreader.WhitespaceHandling = WhitespaceHandling.None
         
         ' Create the XmlValidatingReader over the XmlTextReader.
         ' Set the reader to not expand general entities.
         reader = New XmlValidatingReader(txtreader)
         reader.ValidationType = ValidationType.None
         reader.EntityHandling = EntityHandling.ExpandCharEntities
         
         reader.MoveToContent()
         ' Move to the root element.
         reader.Read()
         ' Move to the title start tag.
         reader.Skip()
         ' Skip the title element.
         ' Read the miscellaneous start tag.  The reader is now positioned on
         ' the entity reference node.
         reader.ReadStartElement()
         
         ' Because EntityHandling is set to ExpandCharEntities, you must call 
         ' ResolveEntity to expand the entity.  The entity replacement text is 
         ' then parsed and returned as a child node.
         Console.WriteLine("Expand the entity...")
         reader.ResolveEntity()
         
         Console.WriteLine("The entity replacement text is returned as a text node.")
         reader.Read()
         Console.WriteLine("NodeType: {0} Value: {1}", reader.NodeType, reader.Value)
         
         Console.WriteLine("An EndEntity node closes the entity reference scope.")
         reader.Read()
         Console.WriteLine("NodeType: {0} Name: {1}", reader.NodeType, reader.Name)
      
      Finally
         If Not (txtreader Is Nothing) Then
            txtreader.Close()
         End If
         If Not (reader Is Nothing) Then
            reader.Close()
         End If
      End Try
   End Sub
   ' Main
End Class
' Sample
[C#]
using System;
using System.IO;
using System.Xml;

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

     try
     {
       // Create and load the XmlTextReader with the XML file.
       txtreader = new XmlTextReader("book1.xml");
       txtreader.WhitespaceHandling = WhitespaceHandling.None;

       // Create the XmlValidatingReader over the XmlTextReader.
       // Set the reader to not expand general entities.
       reader = new XmlValidatingReader(txtreader);
       reader.ValidationType = ValidationType.None;
       reader.EntityHandling = EntityHandling.ExpandCharEntities;

       reader.MoveToContent();  
       // Move to the root element.
       reader.Read();  
       // Move to the title start tag.
       reader.Skip();  
       // Skip the title element.
      
       // Read the miscellaneous start tag.  The reader is now positioned on
       // the entity reference node.
       reader.ReadStartElement(); 

       // Because EntityHandling is set to ExpandCharEntities, you must call 
       // ResolveEntity to expand the entity.  The entity replacement text is 
       // then parsed and returned as a child node.
       
       Console.WriteLine("Expand the entity...");
       reader.ResolveEntity();  

       Console.WriteLine("The entity replacement text is returned as a text node.");
       reader.Read();  
       Console.WriteLine("NodeType: {0} Value: {1}", reader.NodeType ,reader.Value);

       Console.WriteLine("An EndEntity node closes the entity reference scope.");
       reader.Read();
       Console.WriteLine("NodeType: {0} Name: {1}", reader.NodeType,reader.Name);
     
    }
    finally
    {
       if (txtreader != null)
         txtreader.Close();
       if (reader != null)
         reader.Close();
    }
  }
}

以下描述要驗證的輸入檔 book1.xml 的內容。

<?xml version='1.0' ?>
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
<book>
  <title>Pride And Prejudice</title>
  <misc>&h;</misc>
</book>

請參閱

使用結構描述驗證 XML | XmlValidatingReader.ResolveEntity 方法