次の方法で共有


XmlValidatingReader.MoveToAttribute メソッド

指定した属性へ移動します。

オーバーロードの一覧

指定したインデックスを持つ属性に移動します。

[Visual Basic] Overloads Overrides Public Sub MoveToAttribute(Integer)

[C#] public override void MoveToAttribute(int);

[C++] public: void MoveToAttribute(int);

[JScript] public override function MoveToAttribute(int);

指定した名前を持つ属性へ移動します。

[Visual Basic] Overloads Overrides Public Function MoveToAttribute(String) As Boolean

[C#] public override bool MoveToAttribute(string);

[C++] public: bool MoveToAttribute(String*);

[JScript] public override function MoveToAttribute(String) : Boolean;

指定したローカル名および名前空間 URI の属性へ移動します。

[Visual Basic] Overloads Overrides Public Function MoveToAttribute(String, String) As Boolean

[C#] public override bool MoveToAttribute(string, string);

[C++] public: bool MoveToAttribute(String*, String*);

[JScript] public override function MoveToAttribute(String, String) : Boolean;

使用例

[Visual Basic, C#, C++] テキスト ノードおよびエンティティ参照ノードを持つ属性を読み取る例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、MoveToAttribute のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlValidatingReader = Nothing
        
        Try
            'Create the XML fragment to be parsed.
            Dim xmlFrag As String = "<book genre='novel' misc='sale-item &h; 1987'></book>"
            
            'Create the XmlParserContext.
            Dim context As XmlParserContext
            Dim subset As String = "<!ENTITY h 'hardcover'>"
            context = New XmlParserContext(Nothing, Nothing, "book", Nothing, Nothing, 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 Then
                    'To expand the entity, call ResolveEntity.
                    Console.WriteLine("{0} {1}", reader.NodeType, reader.Name)
                Else
                    Console.WriteLine("{0} {1}", reader.NodeType, reader.Value)
                End If
            End While
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub 'Main
End Class 'Sample 

[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

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlValidatingReader* reader = 0;

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

      //Create the XmlParserContext.
      XmlParserContext* context;
      String* subset = S"<!ENTITY h 'hardcover'>";
      context = new XmlParserContext(0, 0, S"book", 0, 0, subset, S"", S"", 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(S"misc");
      while (reader->ReadAttributeValue()){
         if (reader->NodeType==XmlNodeType::EntityReference)
            //To expand the entity, call ResolveEntity.
            Console::WriteLine(S"{0} {1}", __box(reader->NodeType), reader->Name);
         else
            Console::WriteLine(S"{0} {1}", __box(reader->NodeType), reader->Value);
      } 
   } 
   __finally 
   {
      if (reader != 0)
         reader->Close();
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

XmlValidatingReader クラス | XmlValidatingReader メンバ | System.Xml 名前空間