英語で読む

次の方法で共有


XmlValidatingReader.ReadAttributeValue メソッド

定義

属性値を解析して、1 つ以上の TextEntityReferenceEndEntity の各ノードに格納します。

C#
public override bool ReadAttributeValue ();

戻り値

Boolean

返すノードがある場合は true

初めて呼び出すときにリーダーの位置が属性ノード上にない場合、またはすべての属性値が読み込まれている場合は false

misc="" などの空の属性は、値 String.Empty を持つ単一のノードと一緒に true を返します。

次の例では、テキストおよびエンティティ参照ノードを含む属性を読み取ります。

C#
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlValidatingReader reader = null;

    try
    {
       //Create the XML fragment to be parsed.
       string xmlFrag ="<book genre='novel' misc='sale-item &h; 1987'></book>";

       //Create the XmlParserContext.
       XmlParserContext context;
       string subset = "<!ENTITY h 'hardcover'>";
       context = new XmlParserContext(null, null, "book", null, null, subset, "", "", XmlSpace.None);

       //Create the reader and set it to not expand general entities.
       reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
       reader.ValidationType = ValidationType.None;
       reader.EntityHandling = EntityHandling.ExpandCharEntities;

       //Read the misc attribute. Because EntityHandling is set to
       //ExpandCharEntities, the attribute is parsed into multiple text
       //and entity reference nodes.
       reader.MoveToContent();
       reader.MoveToAttribute("misc");
       while (reader.ReadAttributeValue()){
          if (reader.NodeType==XmlNodeType.EntityReference)
            //To expand the entity, call ResolveEntity.
            Console.WriteLine("{0} {1}", reader.NodeType, reader.Name);
          else
             Console.WriteLine("{0} {1}", reader.NodeType, reader.Value);
        }
     }
     finally
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class

注釈

注意

このXmlValidatingReaderクラスは、.NET Framework 2.0 では廃止されています。 クラスとメソッドを使用してXmlReaderSettings、検証XmlReaderインスタンスをCreate作成できます。 詳細については、XmlReader のリファレンス ページの「解説」を参照してください。

このメソッドは、属性値を構成するテキストまたはエンティティ参照ノードを読み取るために呼び出 MoveToAttribute した後に使用します。 Depth属性値ノードの 1 つプラス属性ノードの深さです。 Depth一般的なエンティティ参照にステップ インまたはステップ アウトすると、インクリメントとデクリメントが 1 ずつ行われます。

たとえば、次の XML があるとします。 <test name="a &b; c"/>

ここで、エンティティ b はドキュメント型定義 (DTD) で次のように定義されます。 <!ENTITY b "123">

ExpandCharEntities設定されている場合EntityHandling、次の C# コードは属性値を 2 つのテキスト ノードと 1 つのエンティティ参照ノードとして返します。

C#
reader.MoveToAttribute("name");  
  while (reader.ReadAttributeValue())  
  {  
  if (reader.NodeType == XmlNodeType.Text)  
  {  
  // at this point reader.Value == "a " or " c"  
  }  
  else if (reader.NodeType == XmlNodeType.EntityReference)  
  {  
  // at this point reader.Name == "b"  
  reader.ResolveEntity();  
  while (reader.ReadAttributeValue() &&  
  reader.NodeType != XmlNodeType.EndEntity)  
  {  
  // reader.Value == "123"  
  }  
  }  
    }  

適用対象

製品 バージョン
.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

こちらもご覧ください