本文介绍了在Web浏览器中返回HtmlElement的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Web浏览器中获取所有属性.当前,我正在使用GetAttribute(),但是通过这种方式,我需要知道属性的名称.想象一下,我不知道我的网络浏览器中有什么.我的C#代码:

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.

推荐答案

我还没有尝试过,但是我想这可能是解决方案或第一步.首先,您必须参考microsoft.mshtml

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

这篇关于在Web浏览器中返回HtmlElement的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:34