2013-02-08 50 views
0

基本上,我想與其他標籤,以取代HTML標記在C#中的其他標籤屬性,爲例:取代HTML標籤與

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

在開始時,我用

convertedHtml = html.replace("</br>","<LineBreak/>"); 

的問題這種方法是要求管理所有情況,我想要一個泛型類。例如,對於此標籤內容,此方法不可行:

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

如何解決此問題?

編輯:注意我不知道哪些屬性在標籤中。我想替換包含「p」,「/ p」,「br」,「b」的標籤...

回答

0

您應該使用正則表達式來解決此問題。更多信息請見this site。它會爲您提供區分大小寫/不靈敏匹配的選項。

1

也許你可以使用HTML敏捷性包(http://htmlagilitypack.codeplex.com/

您可以通過的NuGet得到它,它可以讓你做使用XPath得到一個HTMLDOC節點列表...你可以再通過這些列表循環並做每個節點的東西...

+0

+1類似的問題已經被問了很多次在這個網站,那就是這樣做的首選方式。 –

0

我看了一個我的老項目,在那裏我做了類似的事情。

看看這個方法我已經使用:

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"); 
     } 
    } 

我不知道是否符合您的要求,但它可能是如何解決它的想法。當您刪除從HTML標籤的屬性,你可以使用舊的方法convertedHtml = html.replace("</br>","<LineBreak/>");

0

你可以嘗試一些簡單的字符串操作,不需要額外的namaspaces和工具包括:

看到這個例子,也許可以解決你的問題:

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); 

對不起糟糕的代碼,也許你可以通過簡單地做.ToLower()一次,而不是在每一個如果語句來提高。此外,我沒有檢查不好的標籤,代碼只是假定html有效。

JUST編輯的BIT

 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);