XmlTextReader.Normalization プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
空白と属性値を正規化するかどうかを示す値を取得または設定します。
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 です。
例外
リーダーを閉じたときにこのプロパティを設定する (ReadState が ReadState.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
注釈
Note
新しい機能を利用するには、XmlReader メソッドを使用してXmlReader.Create インスタンスを作成することをお勧めします。
このプロパティはいつでも変更でき、次の読み取り操作で有効になります。
Note
XmlTextReaderを使用してXmlValidatingReaderを構築する場合、属性値を正規化するには、Normalizationを true に設定する必要があります。
Normalizationを false に設定すると、数値エンティティの文字範囲チェックも無効になります。 その結果、 �などの文字エンティティが許可されます。
属性値の正規化について次に説明します。
文字参照の場合は、参照される文字を属性値に追加します。
エンティティ参照の場合は、エンティティの置換テキストを再帰的に処理します。
空白文字 (#x20、#xD、#xA、#x9) の場合は、正規化された値に #x20 を追加します。 (外部解析されたエンティティの一部である "#xD#xA" シーケンスまたは内部解析されたエンティティのリテラル エンティティ値に対して追加される #x20 は 1 つだけです)。
正規化された値に追加して、他の文字を処理します。
宣言された値が CDATA でない場合は、先頭と末尾のスペース (#x20) 文字を破棄し、スペース (#x20) 文字のシーケンスを 1 つのスペース (#x20) 文字に置き換えます。
XmlTextReaderは、属性または CDATA 正規化のみを実行します。
XmlValidatingReader内でラップされない限り、DTD 固有の正規化は行われません。
正規化の詳細については、W3C XML 1.0 の推奨事項を参照してください。