XmlValidatingReader.ResolveEntity メソッド
EntityReference ノードのエンティティ参照を解決します。
Overrides Public Sub ResolveEntity()
[C#]
public override void ResolveEntity();
[C++]
public: void ResolveEntity();
[JScript]
public override function ResolveEntity();
例外
例外の種類 | 条件 |
---|---|
InvalidOperationException | リーダーが EntityReference ノードに配置されません。 |
解説
リーダーが EntityReference ノード (XmlNodeType.EntityReference) に配置されている場合は、このメソッドを呼び出した後に Read を呼び出すと、エンティティの置換テキストが解析されます。エンティティ置換テキストが完成すると、 EndEntity ノードが返され、エンティティ参照スコープを閉じます。
メモ このメソッドを呼び出すと、エンティティが属性値の一部の場合は、 ReadAttributeValue を呼び出して、エンティティを調べる必要があります。
使用例
[Visual Basic, C#, C++] ResolveEntity を使用して一般エンティティを展開する例を次に示します。
Option Strict
Option Explicit
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 title start tag.
reader.Skip() 'Skip the title element.
'Read the misc 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 (reader Is Nothing) Then
reader.Close()
End If
End Try
End Sub 'Main
[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 title start tag.
reader.Skip(); //Skip the title element.
//Read the misc 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 (reader != null)
reader.Close();
}
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlValidatingReader* reader = 0;
XmlTextReader* txtreader = 0;
try
{
//Create and load the XmlTextReader with the XML file.
txtreader = new XmlTextReader(S"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 title start tag.
reader->Skip(); //Skip the title element.
//Read the misc 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(S"Expand the entity...");
reader->ResolveEntity();
Console::WriteLine(S"The entity replacement text is returned as a text node.");
reader->Read();
Console::WriteLine(S"NodeType: {0} Value: {1}", __box(reader->NodeType), reader->Value);
Console::WriteLine(S"An EndEntity node closes the entity reference scope.");
reader->Read();
Console::WriteLine(S"NodeType: {0} Name: {1}", __box(reader->NodeType), reader->Name);
}
__finally
{
if (reader != 0)
reader->Close();
}
}
この例では、入力として、 book1.xml というファイルを使用しています。
<?xml version='1.0' ?>
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
<book>
<title>Pride And Prejudice</title>
<misc>&h;</misc>
</book>
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
XmlValidatingReader クラス | XmlValidatingReader メンバ | System.Xml 名前空間 | EntityHandling