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

属性值

true 规范化;否则,为 false. 默认值为 false

例外

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

示例

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

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

注解

注释

建议您使用XmlReader方法创建XmlReader.Create实例,以利用新功能。

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

注释

如果使用 XmlTextReader 构造一个 XmlValidatingReader规范化属性值, Normalization 则必须设置为 true

如果 设置为/>,则它还会禁用数字实体的字符范围检查。 因此,允许字符实体,例如 &#0;

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

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

  • 对于实体引用,以递归方式处理实体的替换文本。

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

  • 通过将其他字符追加到规范化值来处理其他字符。

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

XmlTextReader唯一执行属性或 CDATA 规范化。 除非包装在一个 XmlValidatingReader. 中,否则它不执行特定于 DTD 的规范化。

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

适用于

另请参阅