XmlValidatingReader.EntityHandling 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,表示讀取器如何處理實體。
public:
property System::Xml::EntityHandling EntityHandling { System::Xml::EntityHandling get(); void set(System::Xml::EntityHandling value); };
public System.Xml.EntityHandling EntityHandling { get; set; }
member this.EntityHandling : System.Xml.EntityHandling with get, set
Public Property EntityHandling As EntityHandling
屬性值
其中一個 EntityHandling 值。 若未指定 EntityHandling
,則會預設為 EntityHandling.ExpandEntities。
例外狀況
指定無效的值。
範例
下列範例會 ResolveEntity
使用 方法來展開一般實體。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
XmlValidatingReader^ reader = nullptr;
XmlTextReader^ txtreader = nullptr;
try
{
//Create and load the XmlTextReader with the XML file.
txtreader = gcnew XmlTextReader( "book1.xml" );
txtreader->WhitespaceHandling = WhitespaceHandling::None;
//Create the XmlValidatingReader over the XmlTextReader.
//Set the reader to not expand general entities.
reader = gcnew 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 != nullptr )
reader->Close();
}
}
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();
}
}
}
Option Strict
Option Explicit
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
End Class
此範例會使用 檔案 book1.xml
,作為輸入。
<?xml version='1.0' ?>
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
<book>
<title>Pride And Prejudice</title>
<misc>&h;</misc>
</book>
備註
注意
類別 XmlValidatingReader 在 .NET Framework 2.0 中已過時。 您可以使用 類別和 Create 方法建立驗證 XmlReader 實例 XmlReaderSettings 。 如需詳細資訊,請參閱 XmlReader 參考頁面的<備註>一節。
這個屬性可以變更,並在下一次 Read 呼叫之後生效。
當 設定為 ExpandCharEntities
時 EntityHandling
,屬性值只會部分正規化。 讀取器會將每個個別文位元組點與相鄰實體參考節點的內容分開正規化。
為了說明實體處理模式之間的差異,請考慮下列 XML:
<!DOCTYPE doc [<!ENTITY num "123">]>
<doc> A # </doc>
當 EntityHandling
設定為 ExpandEntities
「doc」 元素節點時,包含一個具有展開實體文字的文位元組點:
深度 | NodeType | 名稱 | 值 |
---|---|---|---|
1 | Text | A 123 |
當 設定為 ExpandCharEntities
,且 WhitespaceHandling 設定為 [重大] 或 [全部] 時 EntityHandling
,「doc」 元素會展開字元實體,並將一般實體傳回為節點:
深度 | NodeType | 名稱 | 值 |
---|---|---|---|
1 | Text | A | |
1 | EntityReference | num | |
1 | SignificantWhitespace |