Return all attributes of an HtmlElement in Web browser

ⅰ亾dé卋堺 提交于 2019-12-07 01:58:36

问题


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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/16631820/return-all-attributes-of-an-htmlelement-in-web-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!