C# 用C中的其他标记替换属性为html标记#

C# 用C中的其他标记替换属性为html标记#,c#,replace,tags,html,C#,Replace,Tags,Html,基本上,我想用其他标记替换html标记,例如: </br> --> <LineBreak/> <p> --> <Paragraph> --> --> 一开始,我用 convertedHtml = html.replace("</br>","<LineBreak/>"); convertedHtml=html.replace(“”,”); 这个方法的问题是需要管理所有案例,我想要一个泛型类。例如,此方法不适

基本上,我想用其他标记替换html标记,例如:

</br> --> <LineBreak/>
<p> --> <Paragraph>

--> -->
一开始,我用

convertedHtml = html.replace("</br>","<LineBreak/>");
convertedHtml=html.replace(“
”,”);
这个方法的问题是需要管理所有案例,我想要一个泛型类。例如,此方法不适用于此标签内容:

<p class="foo"> --> <Paragraph>
<p id="bar"> --> <Paragraph>
.....

-->

--> .....

我如何解决这个问题


编辑:注意,我事先不知道标签中有哪些属性。我想要替换包含“p”、“p”、“br”、“b”的标记,…

您应该使用正则表达式来解决此问题。有关更多信息,请访问。它将为您提供区分大小写/不区分大小写的匹配选项。

也许您可以使用HTML Agility Pack()


您可以通过NuGet获取它,它允许您使用xPath从htmlDoc获取节点列表。。。然后,您可以循环浏览这些列表并对每个节点执行操作…

我在一个较旧的项目中查找了一些类似的操作

看看我一直在使用的这个方法:

    private static Regex _validAttributeOrTagNameRegEx = 
                       new Regex(@"^\w+$", RegexOptions.Compiled |RegexOptions.IgnoreCase);
        private const string STR_RemoveHtmlAttributeRegex = 
                           @"(?<=<)([^/>]+)(\s{0}=['""][^'""]+?['""])([^/>]*)(?=/?>|\s)";
    public static string RemoveHtmlAttribute(this string input, string attributeName) {
       if (_validAttributeOrTagNameRegEx.IsMatch(attributeName)) {
          Regex reg = new Regex(string.Format(STR_RemoveHtmlAttributeRegex, attributeName),
             RegexOptions.IgnoreCase);
          return reg.Replace(input, item => item.Groups[1].Value + item.Groups[3].Value);
       } else {
          throw new ArgumentException("Not a valid HTML attribute name", "attributeName");
       }
    }
private static Regex\u validAttributeOrTagNameRegEx=
新的正则表达式(@“^\w+$”,RegexOptions.Compiled | RegexOptions.IgnoreCase);
私有常量字符串STR_removeThlatTributtereGex=

@(?您可以尝试一些简单的字符串操作,不包括其他NAMASPACE和工具:

看看这个例子,也许它能解决你的问题:

string html = string.Concat("<p class=\"foo\">", 
                             "<p class=\"bar\">",
                             "<p>",
                             "</br>",
                             "<P>",
                             "</BR>"); // tags can be upper case as well

string strAux = html;
int tagOpenedAt=-1, tagClosedAt=-1;
bool isError = false;

do
{
   tagOpenedAt = strAux.IndexOf('<');
   tagClosedAt = strAux.IndexOf('>');
   if(tagOpenedAt<tagClosedAt)
   {
       string fullTag = strAux.Substring(tagOpenedAt, tagClosedAt - tagOpenedAt + 1);

       //<p> --> <Paragraph>
       if (fullTag.ToLower().Equals("<p>") || fullTag.ToLower().StartsWith("<p ")) 
           html = html.Replace(fullTag, "<Paragraph>");

       //</br> --> <LineBreak/>
       if (fullTag.ToLower().Equals("</br>")) 
           html = html.Replace(fullTag, "<LineBreak/>");

       //more if conditions as you need them

       strAux = strAux.Substring(tagClosedAt + 1);
   }
   else
   {
       isError = true;
   }
} 
while (tagOpenedAt>-1 && tagClosedAt>-1 && !isError);
string html=string.Concat(“

”, “

”, “”, “
”, “

”, “
”;//标记也可以是大写 字符串strAux=html; int tagOpenedAt=-1,tagClosedAt=-1; bool-isError=false; 做 { tagOpenedAt=strAux.IndexOf(“”); if(tagOpenedAt)


如果(fullTag.ToLower().Equals(“”)| | fullTag.ToLower().StartsWith(“+1类似的问题在本网站上被多次询问,这是首选的解决方法。
       string html = string.Concat("<p class=\"foo\">","\n",
                                    "<p class=\"bar\">", "\n",
                                    "<p>", "\n",
                                    "</br>", "\n",
                                    "<P>", "\n",
                                    "</BR>");

        Console.WriteLine("HTML is :\n{0}\n", html);

        string strAux = html;
        int tagOpenedAt=-1, tagClosedAt=-1;
        bool isError = false;

        do
        {
            tagOpenedAt = strAux.IndexOf('<');
            tagClosedAt = strAux.IndexOf('>');
            if(tagOpenedAt < tagClosedAt)
            {
                string _fullTag = strAux.Substring(tagOpenedAt, tagClosedAt - tagOpenedAt + 1);
                string _lower = _fullTag.ToLower();
                string _replace = null;

                //<p> --> <Paragraph>
                if (_lower.Equals("<p>") || _lower.StartsWith("<p "))
                    _replace = "<Paragraph>";

                //</br> --> <LineBreak/>
                if (_lower.Equals("</br>")) 
                    _replace = "<LineBreak/>";

                //more if conditions as you need them

                if(_replace != null)
                {
                    html = html.Replace(_fullTag, _replace);
                    Console.WriteLine("Replaced {0} with {1}", _fullTag, _replace);
                }

                strAux = strAux.Substring(tagClosedAt + 1);
            }
            else
            {
                isError = true;
            }
        } 
        while (tagOpenedAt>-1 && tagClosedAt>-1 && !isError);

    Console.WriteLine("\nNew html is :\n{0}",html);