2012-07-06 2 views
0

특정 장소에서 Word HTML 태그를 제거해야합니다. 순간 나는이 일을 해요 :스트립 Word HTML 태그

public string CleanWordStyle(string html) 
{ 
    StringCollection sc = new StringCollection(); 
    sc.Add(@"<table\b[^>]*>(.*?)</table>"); 
    sc.Add(@"(<o:|</o:)[^>]+>"); 
    sc.Add(@"(<v:|</v:)[^>]+>"); 
    sc.Add(@"(<st1:|</st1:)[^>]+>"); 
    sc.Add(@"(mso-bidi-|mso-fareast|mso-spacerun:|mso-list: ign|mso-ascii|mso-hansi|mso-ansi|mso-element|mso-special|mso-highlight|mso-border|mso-yfti|mso-padding|mso-background|mso-tab|mso-width|mso-height|mso-pagination|mso-theme|mso-outline)[^;]+;"); 
    sc.Add(@"(font-size|font-family):[^;]+;"); 
    sc.Add(@"font:[^;]+;"); 
    sc.Add(@"line-height:[^;]+;"); 
    sc.Add(@"class=""mso[^""]+"""); 
    sc.Add(@"times new roman&quot;,&quot;serif&quot;;"); 
    sc.Add(@"verdana&quot;,&quot;sans-serif&quot;;"); 
    sc.Add(@"<p> </p>"); 
    sc.Add(@"<p>&nbsp;</p>"); 
    foreach (string s in sc) 
    { 
     html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase); 
    } 
    html = Regex.Replace(html, @"&nbsp;", @"&#160;"); //can not be read by as XmlDocument if not! 
    return html; 
} 

을 지금 내가 sc.Add(@"<p> </p>");<p> 태그 전체 HTML을 제거하고,하지만 내가 원하는 것은 : 나는 테이블 태그를 치면, 그것은 테이블을 돌 때까지 교체 중지해야 끝 태그. 가능한가?

+0

내가 솔루션을 제공하지만, 지금은 다시 생각, 나도 몰라 ... 그냥 텍스트를 유지, 제거하고 단어 형식화하는 당신이 어떻게 보이는지 경우 그러나 HTMLAgilityPack을 사용하는 것이 좋습니다. – Aristos

+0

제 커스터머는 테이블 태그 안의 모든 것을 만져야한다고 생각합니다.하지만 그 밖의 모든 것은 제거되어야합니다. 그것 wasnt 정확하게 내가 찾던 해결책 – Timsen

+0

HTMLAgilityPack을 보아라, 이것은 생각이다, 이것은 당신에게 DOM를 줄 수있다. 그리고 거기에서 당신은 당신이 바라는 부분을 지킬 수있다. – Aristos

답변

0

정규 표현식은 줄 또는 매우 간단한 html 구조체에서 작동 할 수 있습니다.

최소 코드로 작업하려면 실제로 http://htmlagilitypack.codeplex.com/에서 HTMLAgilityPack을 가져와 모든 태그의 내부 값에서 모든 텍스트를 가져옵니다.

으로 그것은 간단하다 :

public string CleanWordStyle(string htmlPage) 
{ 
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
    doc.LoadHtml(htmlPage); 

    return doc.DocumentNode.InnerText; 
} 
+0

모든 자식 노드를 통과하고 stringbuilding에 추가하는 대신 루트 노드의 innerttext를 반환 할 수 있습니다. – jnoreiga

+0

@jnoreiga 수정 해 주셔서 감사합니다. – Aristos

+1

문제 없습니다. 그래도 단어 스타일을 제외하지는 않습니다. 그것은 루트 내부의 모든 HTML을 제거합니다. – jnoreiga