HtmlDocument.GetElementsByTagName(String) Metodo

Definizione

Recuperare una raccolta di elementi con il tag HTML specificato.

public:
 System::Windows::Forms::HtmlElementCollection ^ GetElementsByTagName(System::String ^ tagName);
public System.Windows.Forms.HtmlElementCollection GetElementsByTagName(string tagName);
member this.GetElementsByTagName : string -> System.Windows.Forms.HtmlElementCollection
Public Function GetElementsByTagName (tagName As String) As HtmlElementCollection

Parametri

tagName
String

Nome del tag HTML per gli HtmlElement oggetti da recuperare.

Valori restituiti

La raccolta di elementi che assegnano un tag name è uguale all'argomento tagName .

Esempio

Le pagine HTML spesso usano il META tag per incorporare informazioni arbitrarie sul documento. L'esempio di codice HTML seguente recupera tutti i tag all'interno di META un documento HTML, trova il META tag con il nome Descriptione lo visualizza all'utente. L'esempio di codice richiede che l'applicazione abbia un WebBrowser controllo denominato 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

Si applica a

Vedi anche