共用方式為


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 實例,以利用新功能。

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

注意

如果使用 建構 , 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 建議。

適用於

另請參閱