次の方法で共有


XmlValidatingReader.MoveToAttribute メソッド (String)

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

Overrides Overloads Public Function MoveToAttribute( _
   ByVal name As String _) As Boolean
[C#]
public override bool MoveToAttribute(stringname);
[C++]
public: bool MoveToAttribute(String* name);
[JScript]
public override function MoveToAttribute(
   name : String) : Boolean;

パラメータ

  • name
    属性の限定名。

戻り値

属性が見つかった場合は true 。それ以外の場合は falsefalse の場合、リーダーの位置は変更されません。

解説

このメソッドを呼び出すと、 NameNamespaceURIPrefix の各プロパティは、その属性のプロパティを反映します。

使用例

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

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XmlValidatingReader クラス | XmlValidatingReader メンバ | System.Xml 名前空間 | XmlValidatingReader.MoveToAttribute オーバーロードの一覧