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値としての要素のこの属性の値。 指定した属性がこの要素に存在しない場合は、空の文字列を返します。

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

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 タグの属性ではありません。 GetAttributeSetAttributeを使用して、マネージド ドキュメント オブジェクト モデル (DOM) で公開されていない属性を操作します。

GetAttributeSetAttribute では大文字と小文字が区別されません。

適用対象

こちらもご覧ください