Попробуй вот так (тут сама идея такого разбора):
Код | using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions;
namespace HtmlSimpleParse { public class HtmlHelper { static string GetAttributePattern(string attribute) { return attribute + "\\s*=\\s*[\"|\'](?<" + attribute + ">.*?)[\"|\']"; } static string GetTagAttributesPattern(string tagName) { return "<"+tagName + "(?<attributes>.*?)>"; } public static List<string> CollectTagAttributeStrings(string html, string tagName) { List<string> result = new List<string>(); MatchCollection tagAttributesMatches = Regex.Matches(html, GetTagAttributesPattern(tagName)); foreach(Match match in tagAttributesMatches) { string strTagAttributes = match.Groups["attributes"].Value; if(!string.IsNullOrEmpty(strTagAttributes)) result.Add(strTagAttributes); } return result; } public static string FindAttributeValue(string source, string attribute) { return Regex.Match(source, GetAttributePattern(attribute)).Groups[attribute].Value; } } class Program { static string html = "<table width=\"100%\" cellspacing=\"1\" cellpadding=\"0\">" + "<tr><td><label>Имя:</label>" + "<input class=\"fm fm110\" type=\"text\" name=\"e3fe509\" value=\"123sdfa\" maxlength=\"15\"> <span class=\"e f7\"></span>" + "</td></tr>" + "<tr><td><label>Пароль:</label>" + "<input class=\"fm fm110\" type=\"password\" name=\"e9a7f22\" value=\"\" maxlength=\"20\"> <span class=\"e f7\"></span>"; static void Main(string[] args) { List<string> attributeStrings = HtmlHelper.CollectTagAttributeStrings(html, "input"); foreach(string atrStr in attributeStrings) { Console.WriteLine("Class = " + HtmlHelper.FindAttributeValue(atrStr, "class")); Console.WriteLine("Type = " + HtmlHelper.FindAttributeValue(atrStr, "type")); Console.WriteLine("Name = " + HtmlHelper.FindAttributeValue(atrStr, "name")); } } } }
|
|