XmlTextReader.Normalization 屬性

定義

取得或設定值,指示是否將空白字元與屬性值正常化。

C#
public bool Normalization { get; set; }

屬性值

Boolean

若要正常化,則為 true,否則為 false。 預設為 false

例外狀況

在讀取器關閉時設定這個屬性 (ReadStateReadState.Closed)。

範例

下列範例顯示已開啟和關閉正規化的讀取器行為。

C#
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();
  }
}

備註

備註

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

這個屬性可以隨時變更,並在下一個讀取作業上生效。

備註

如果使用 建構 , XmlTextReader 將 屬性值正規化, Normalization 則必須設定為 trueXmlValidatingReader

如果 Normalization 設定為 false ,這也會停用數值實體的字元範圍檢查。 因此,允許字元實體,例如 &#0;

下列描述屬性值正規化:

  • 對於字元參考,請將參考的字元附加到屬性值中。

  • 對於實體參考,請以遞迴處理實體的取代文字。

  • 如需空白字元 (#x20、#xD、#xA、#x9) ,請將#x20附加至標準化值。 (只有單一#x20會針對屬於外部剖析實體的 「#xD#xA」 序列附加,或是內部剖析實體的常值實體值。)

  • 處理其他字元,請將它們附加至正規化值中。

  • 如果宣告的值不是 CDATA,請捨棄任何開頭和尾端空格 (#x20) 字元,並以單一空格 (#x20) 字元取代空格 (#x20) 字元序列。

只會 XmlTextReader 執行屬性或 CDATA 正規化。 除非 包裝在 內 XmlValidatingReader ,否則不會執行 DTD 特定的正規化。

如需標準化的進一步討論,請參閱 W3C XML 1.0 建議。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

另請參閱