XmlTextReader.Normalization 属性

定义

获取或设置一个值,该值指示是否规范化空白区域和属性值。

public:
 property bool Normalization { bool get(); void set(bool value); };
public bool Normalization { get; set; }
member this.Normalization : bool with get, set
Public Property Normalization As Boolean

属性值

Boolean

如果要进行规范化,则为 true;否则为 false。 默认值为 false

例外

当读取器关闭(ReadStateReadState.Closed)时,设置此属性。

示例

以下示例显示打开并关闭规范化的读取器行为。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XML fragment to be parsed.
   String^ xmlFrag = "<item attr1='  test A B C\n"
   "1 2 3'/>\n"
   "<item attr2=''/>\n";
   
   // Create the XmlNamespaceManager.
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( gcnew NameTable );
   
   // Create the XmlParserContext.
   XmlParserContext^ context = gcnew XmlParserContext( nullptr,nsmgr,nullptr,XmlSpace::Preserve );
   
   // Create the reader.
   XmlTextReader^ reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );
   
   // Show attribute value normalization.
   reader->Read();
   reader->Normalization = false;
   Console::WriteLine( "Attribute value: {0}", reader->GetAttribute( "attr1" ) );
   reader->Normalization = true;
   Console::WriteLine( "Attribute value: {0}", reader->GetAttribute( "attr1" ) );
   
   // Set Normalization back to false.  This allows the reader to accept
   // character entities in the � to  range.  If Normalization had
   // been set to true, character entities in this range throw an exception.
   reader->Normalization = false;
   reader->Read();
   reader->MoveToContent();
   Console::WriteLine( "Attribute value: {0}", reader->GetAttribute( "attr2" ) );
   
   // Close the reader.
   reader->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample{

  public static void Main(){

    // Create the XML fragment to be parsed.
    string xmlFrag  =
    @"<item attr1='  test A B C
        1 2 3'/>
      <item attr2=''/>";

    // Create the XmlNamespaceManager.
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

    // Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.Preserve);

    // Create the reader.
    XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

    // Show attribute value normalization.
    reader.Read();
    reader.Normalization = false;
    Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));
    reader.Normalization = true;
    Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"));

    // Set Normalization back to false.  This allows the reader to accept
    // character entities in the � to  range.  If Normalization had
    // been set to true, character entities in this range throw an exception.
    reader.Normalization = false;
    reader.Read();
    reader.MoveToContent();
    Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr2"));

    // Close the reader.
    reader.Close();
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    ' Create the XML fragment to be parsed.
    Dim xmlFrag as string = "<item attr1='  test A B C " + Chr(10) & _
                            "   1 2 3'/>" + Chr(10) & _
                            "<item attr2=''/>"
                    

    ' Create the XmlNamespaceManager.
    Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(new NameTable())

    ' Create the XmlParserContext.
    Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.Preserve)

    ' Create the reader.
    Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)

    ' Show attribute value normalization.
    reader.Read()
    reader.Normalization = false
    Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"))
    reader.Normalization = true
    Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr1"))

    ' Set Normalization back to false.  This allows the reader to accept
    ' character entities in the � to  range.  If Normalization had
    ' been set to true, character entities in this range throw an exception.
    reader.Normalization = false
    reader.Read()
    reader.MoveToContent()
    Console.WriteLine("Attribute value:{0}", reader.GetAttribute("attr2"))
  
    ' Close the reader.
    reader.Close()     
  
  end sub
end class

注解

备注

从 .NET Framework 2.0 开始,我们建议使用XmlReader.Create该方法来利用新功能来创建XmlReader实例。

可以随时更改此属性,并对下一个读取操作生效。

备注

If the XmlTextReader is used to construct an XmlValidatingReader, to normalize attribute values, Normalization must be set to true.

If Normalization is set to false, this also disables character range checking for numeric entities. 因此,允许字符实体,例如 &#0;

下面介绍了属性值规范化:

  • 对于字符引用,将所引用的字符追加到属性值。

  • 对于实体引用,循环处理该实体的替换文本。

  • 对于空格字符 (#x20、#xD、#xA、#x9) ,请将#x20追加到规范化值。 (只有一个#x20追加到作为外部分析实体的一部分的“#xD#xA”序列或内部分析实体的文本实体值。)

  • 通过将其他字符追加到已标准化的值处理这些字符。

  • 如果声明的值不是 CDATA,请放弃任何前导空格和尾随空格 (#x20) 字符,并将空格 (#x20) 字符序列替换为单个空格 (#x20) 字符。

XmlTextReader仅执行属性或 CDATA 规范化。 除非包装在 XmlValidatingReaderDTD 内,否则它不执行特定于 DTD 的规范化。

有关规范化的进一步讨论,请参阅 W3C XML 1.0 建议。

适用于

另请参阅