次の方法で共有


XmlValidatingReader を使用したエンティティの解決

EntityHandling プロパティは、XmlValidatingReader がエンティティを処理する方法を定義します。

ExpandEntities

EntityHandling プロパティが ExpandEntities に設定されている場合、XmlValidatingReader は、XML ドキュメント内のすべてのエンティティを展開します。エンティティのテキストが展開されるため、XmlNodeTypeEntityReference は表示されません。これは、既定の設定です。

ExpandCharEntities

EntityHandling プロパティが ExpandCharEntities に設定されている場合、XmlValidatingReader は、文字エンティティを展開し、一般エンティティを 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 メソッド