Return all attributes of an HtmlElement in Web browser
Asked Answered
A

1

6

I need to get all of the attributes from my webbrowser.currently,I am using GetAttribute() but this way,I need to know the name of the attributes. Imagine I do not know what is in my webbrowser. My C# code:

        StringWriter strWriter = new StringWriter();            
        XmlWriter xWriter = XmlWriter.Create(strWriter, new XmlWriterSettings() { Indent = true });
        xWriter.WriteStartElement("Items");
        foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("TEXTAREA"))
        {
            xWriter.WriteStartElement("Item");
            xWriter.WriteElementString("GUID", el.Id);
            xWriter.WriteElementString("Type", el.GetAttribute("type").ToUpper());
            xWriter.WriteElementString("Name", el.Name);
            xWriter.WriteElementString("Value", el.GetAttribute("value"));
            xWriter.WriteElementString("MaxLength", el.GetAttribute("maxlength"));
            xWriter.WriteEndElement();
        }

I have searched a lot but I did not find any thing useful.

Akvavit answered 19/5, 2013 at 5:45 Comment(1)
I also tried using Html Agility Pack(htmlagilitypack.codeplex.com/wikipage?title=Examples) but it was not such a good solution.Akvavit
D
2

I haven't tried it, but I guess this could be a solution or the first step. First, you have to reference to microsoft.mshtml

foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("TEXTAREA"))
{

    HTMLTextAreaElement textarea = (HTMLTextAreaElement)el.DomElement;

    xWriter.WriteStartElement("Item");
    xWriter.WriteElementString("GUID", el.Id);

    foreach (var attribute in textarea.attributes)
    {
         String name = attribute.name;
         String value = attribute.value;

         xWriter.WriteElementString(name, value);
    }

    xWriter.WriteEndElement();
}
Dearly answered 19/5, 2013 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.