HtmlElement.GetAttribute(String) メソッド

定義

要素の名前付き属性の値を取得します。

public:
 System::String ^ GetAttribute(System::String ^ attributeName);
public string GetAttribute (string attributeName);
member this.GetAttribute : string -> string
Public Function GetAttribute (attributeName As String) As String

パラメーター

attributeName
String

属性の名前。 この引数では、大文字と小文字が区別されません。

戻り値

String

String 値としての、要素のこの属性の値。 指定された属性がこの要素に存在しない場合は、空の文字列を返します。

次のコード例では、HTML ドキュメント内のすべてのタグをMETA取得し、その名前Descriptionを持GetAttributeつタグをMETA検索します。 この例では、アプリケーションにという名前WebBrowser1のコントロールがWebBrowser必要です。

private void DisplayMetaDescription()
{
    if (webBrowser1.Document != null)
    {
        HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("META");
        foreach (HtmlElement elem in elems)
        {
            String nameStr = elem.GetAttribute("name");
            if (nameStr != null && nameStr.Length != 0)
            {
                String contentStr = elem.GetAttribute("content");
                MessageBox.Show("Document: " + webBrowser1.Url.ToString() + "\nDescription: " + contentStr);
            }
        }
    }
}
Private Sub DisplayMetaDescription()
    If (WebBrowser1.Document IsNot Nothing) Then
        Dim Elems As HtmlElementCollection 
        Dim WebOC as WebBrowser = WebBrowser1

    Elems = WebOC.Document.GetElementsByTagName("META")

        For Each elem As HtmlElement In Elems
            Dim NameStr As String = elem.GetAttribute("name")

            If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
                If NameStr.ToLower().Equals("description") Then
                    Dim ContentStr As String = elem.GetAttribute("content")
                    MessageBox.Show("Document: " & WebOC.Url.ToString() & vbCrLf & "Description: " & ContentStr)
                End If
            End If
        Next
    End If
End Sub

注釈

HTML の属性は、その要素に対して有効な名前と値のペアです。 HtmlElement は、すべての要素に共通する属性のみを公開し、特定の種類の要素にのみ適用される属性を除外します。 SRC はタグの定義済みの属性 IMG であり、タグには DIV 含まれません。 マネージド ドキュメント オブジェクト モデル (DOM) で公開されていない属性を使用 GetAttribute して SetAttribute 操作します。

GetAttribute 大文字と SetAttribute 小文字は区別されません。

適用対象

こちらもご覧ください