问题
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