XmlNodeReader.MoveToFirstAttribute 方法

定义

移动到第一个属性。

public:
 override bool MoveToFirstAttribute();
public override bool MoveToFirstAttribute ();
override this.MoveToFirstAttribute : unit -> bool
Public Overrides Function MoveToFirstAttribute () As Boolean

返回

如果属性存在(读取器移动到第一个属性),则为 true;否则为 false(读取器的位置不更改)。

示例

以下示例获取根节点的第一个属性的值。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlNodeReader^ reader = nullptr;
   try
   {
      
      //Create and load the XML document.
      XmlDocument^ doc = gcnew XmlDocument;
      doc->LoadXml( "<book genre='novel' ISBN='1-861003-78' publicationdate='1987'></book>" );
      
      //Load the XmlNodeReader 
      reader = gcnew XmlNodeReader( doc );
      
      //Read the genre attribute.
      reader->MoveToContent();
      reader->MoveToFirstAttribute();
      String^ genre = reader->Value;
      Console::WriteLine( "The genre value: {0}", genre );
   }
   finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}
using System;
using System.IO;
using System.Xml;

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

    try
    {
       //Create and load the XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " +
                   "</book>");

       //Load the XmlNodeReader
       reader = new XmlNodeReader(doc);

       //Read the genre attribute.
       reader.MoveToContent();
       reader.MoveToFirstAttribute();
       string genre=reader.Value;
       Console.WriteLine("The genre value: " + genre);
     }
     finally
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlNodeReader = Nothing
        
        Try
            'Create and load the XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<book genre='novel' ISBN='1-861003-78' publicationdate='1987'> " & _
                        "</book>")
            
            'Load the XmlNodeReader 
            reader = New XmlNodeReader(doc)
            
            'Read the genre attribute.
            reader.MoveToContent()
            reader.MoveToFirstAttribute()
            Dim genre As String = reader.Value
            Console.WriteLine("The genre value: " & genre)
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

注解

注意

在 .NET Framework 2.0 中,建议的做法是使用 XmlReaderSettings 类和 Create 方法创建XmlReader实例。 这使你可以充分利用 .NET Framework 中引入的所有新功能。有关详细信息,请参阅参考页中的 XmlReader “备注”部分。

适用于