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>
当 设置为 ExpandEntities
时EntityHandling
,“doc”元素节点包含一个带有展开实体文本的文本节点:
深度 | NodeType | 名称 | 值 |
---|---|---|---|
1 | 文本 | A 123 |
当 EntityHandling
设置为 ExpandCharEntities
时,并且 WhitespaceHandling 设置为“重要”或“全部”时,“doc”元素将展开字符实体,并将常规实体作为节点返回:
深度 | NodeType | 名称 | 值 |
---|---|---|---|
1 | 文本 | A | |
1 | EntityReference | num | |
1 | SignificantWhitespace |