开发者

Best way to convert HTMLto CSHTML

I have a few dozen HTML fo开发者_运维问答rms that I need to convert to ASP.NET MVC Razor partial views (CSHTML). Specifically, I need to convert each to a @Html.TextBox. For example, convert

<input name="text1" style="margin:0pt"></input>

to

@Html.TextBox("text1", String.Empty, new { style = "margin:0pt" })

and then save the file to a new CSHTML file. Of course, the input tag could be coded as

<input .../> or as <input ...></input>

and the tag may or may not have a style attribute.

Anyone know of an automated way that I could do this? The alternative is to manually make the changes, which would be rather time-consuming. I was thinking some regular expression magic would be the way to go, or perhaps using some kind of HTML DOM parser, but I'm not much of a Regex god and I'm not familiar with any parsing components that would help.

Any suggestions would be most appreciated!


This will help you,

    private static void ReplaceInputTagsWithHtmlTextBox(string filePath)
    {
        var input = System.IO.File.ReadAllText(filePath);
        var keyValueRegex = new Regex("\\s*(?<key>[^=]*)=\\s*['\"](?<value>[^'\"]*)['\"]");
        var values = new List<string>();
        var regex = new Regex(@"<input (?<Attributes>[^>]*)>(</input>)?");
        var newContent = regex.Replace(input, m =>
        {
            var keyValueDict = new Dictionary<string, string>();
            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<string, string> 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);
        }
    }

Now jus call it using,

ReplaceInputTagsWithHtmlTextBox("Temp.cshtml");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜