XmlTextReader.Normalization Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy znormalizować białe znaki i wartości atrybutów.

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

Wartość właściwości

Boolean

true do normalizacji; w przeciwnym razie , false. Wartość domyślna to false.

Wyjątki

Ustawienie tej właściwości po zamknięciu czytnika (ReadState to ReadState.Closed).

Przykłady

W poniższym przykładzie pokazano zachowanie czytelnika z włączoną normalizacją, a następnie wyłączone.

#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

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu metody , aby korzystać z XmlReader.Create nowych funkcji.

Ta właściwość może zostać zmieniona w dowolnym momencie i będzie obowiązywać w następnej operacji odczytu.

Uwaga

Jeśli element XmlTextReader jest używany do konstruowania XmlValidatingReaderelementu , aby znormalizować wartości atrybutów, Normalization należy ustawić wartość true.

Jeśli Normalization jest ustawiona wartość false, spowoduje to również wyłączenie sprawdzania zakresu znaków dla jednostek liczbowych. W rezultacie dozwolone są jednostki znaków, takie jak &#0;, .

Poniżej opisano normalizację wartości atrybutów:

  • W przypadku odwołania do znaków dołącz przywołyny znak do wartości atrybutu.

  • W przypadku odwołania do jednostki rekursywnie przetwarzaj tekst zastępczy jednostki.

  • W przypadku znaku odstępu (#x20, #xD, #xA, #x9), dołącz #x20 do znormalizowanej wartości. (Tylko jedna #x20 jest dołączana do sekwencji "#xD#xA", która jest częścią zewnętrznej jednostki analizowanej lub wartości jednostki literału wewnętrznej jednostki analizowanej).

  • Przetwórz inne znaki, dołączając je do znormalizowanych wartości.

  • Jeśli zadeklarowana wartość nie jest wartością CDATA, odrzuć znaki spacji wiodącej i końcowej (#x20) i zastąp sekwencje znaków spacji (#x20) znakiem pojedynczego miejsca (#x20).

Jedyną wartością XmlTextReader jest normalizacja atrybutu lub CDATA. Nie powoduje normalizacji specyficznej XmlValidatingReaderdla dtD, chyba że opakowana w obiekcie .

Zapoznaj się z zaleceniem W3C XML 1.0, aby uzyskać dalszą dyskusję na temat normalizacji.

Dotyczy

Zobacz też