示例#1
0
        private static string ParseFormMessage(string formMessage, Webpage webpage, FormPosting formPosting)
        {
            Regex formRegex    = new Regex(@"\[form\]");
            Regex pageRegex    = new Regex(@"{{page.(.*)}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Regex messageRegex = new Regex(@"{{(.*)}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            formMessage = formRegex.Replace(formMessage, match =>
            {
                TagBuilder list = new TagBuilder("ul");

                foreach (FormValue formValue in formPosting.FormValues)
                {
                    TagBuilder listItem = new TagBuilder("li");

                    TagBuilder title    = new TagBuilder("b");
                    title.InnerHtml    += formValue.Key + ":";
                    listItem.InnerHtml += title.ToString() + " " +
                                          formValue.GetMessageValue();

                    list.InnerHtml += listItem.ToString();
                }

                return(list.ToString());
            });

            formMessage = pageRegex.Replace(formMessage, match =>
            {
                System.Reflection.PropertyInfo propertyInfo =
                    typeof(Webpage).GetProperties().FirstOrDefault(
                        info =>
                        info.Name.Equals(match.Value.Replace("{", "").Replace("}", "").Replace("page.", ""),
                                         StringComparison.OrdinalIgnoreCase));

                return(propertyInfo == null
                    ? string.Empty
                    : propertyInfo.GetValue(webpage,
                                            null).
                       ToString());
            });
            return(messageRegex.Replace(formMessage, match =>
            {
                FormValue formValue =
                    formPosting.FormValues.FirstOrDefault(
                        value =>
                        value.Key.Equals(
                            match.Value.Replace("{", "").Replace("}", ""),
                            StringComparison.
                            OrdinalIgnoreCase));
                return formValue == null
                    ? string.Empty
                    : formValue.GetMessageValue();
            }));
        }