这将帮助你,
private static void ReplaceInputTagsWithHtmlTextBox(string filePath)
{
var input = System.IO.File.ReadAllText(filePath);
var keyValueRegex = new Regex("\\s*(?[^=]*)=\\s*['\"](?[^'\"]*)['\"]");
var values = new List();
var regex = new Regex(@"[^>]*)>()?");
var newContent = regex.Replace(input, m =>
{
var keyValueDict = new Dictionary();
var allAttributes = m.Groups["Attributes"].Value;
keyValueRegex.Replace(m.Groups["Attributes"].Value, mm =>
{
keyValueDict.Add(mm.Groups["key"].Value, mm.Groups["value"].Value);
return null;
});
var keyValues = new StringBuilder();
foreach (KeyValuePair pair in keyValueDict)
{
if (!pair.Key.Equals("id", StringComparison.OrdinalIgnoreCase) && !pair.Key.Equals("name", StringComparison.OrdinalIgnoreCase) && !pair.Key.Equals("value", StringComparison.OrdinalIgnoreCase))//don't include id, name and value
keyValues.Append(pair.Key.Replace("class", "@class") + "= \"" + pair.Value + "\", ");
}
var keyValuesString = keyValues.ToString();
if (keyValuesString.EndsWith(", "))
keyValuesString = keyValuesString.Remove(keyValuesString.Length - 2, 2);
if (!keyValueDict.ContainsKey("name") || keyValueDict["name"] == null)
keyValueDict["name"] = "";
if (!keyValueDict.ContainsKey("value") || keyValueDict["value"] == null)
keyValueDict["value"] = "";
return String.Format("@Html.TextBox(\"{0}\", \"{1}\", new {{ {2} }})\n", keyValueDict["name"], keyValueDict["value"], keyValuesString);
});
using (StreamWriter outfile =new StreamWriter(filePath))
{
outfile.Write(newContent);
}
}
现在强制它采用打电话,
ReplaceInputTagsWithHtmlTextBox("Temp.cshtml");