C # convert Html content to Csv content including table (which focuses on merging rowspan and colspan), p, div element,
Convert Html content to Csv content, including table (which focuses on rowspan and colspan merging), p, div elements, and table cannot contain nested functions.
1 /// <summary> 2 // convert Html content to Csv content including table (focusing on rowspan and colspan merging), p, div element 3 /// </summary> 4 /// <param name = "hrml"> </param> 5 /// <returns> </returns> 6 private string htmlToCsv (string hrml) 7 {8 HtmlAgilityPack. htmlDocument doc = new HtmlAgilityPack. htmlDocument (); 9 doc. loadHtml (hrml); 10 StringBuilder sbLines = new StringBuilder (); 11 HtmlAgilityPack. htmlNodeCollection tList = doc. documentNode. SelectNodes ("// table"); 12 if (tList! = Null) 13 {14 foreach (HtmlAgilityPack. htmlNode table in tList) 15 {16 sbLines. appendLine ("# flag_table #,"); 17 HtmlAgilityPack. htmlNodeCollection rows = table. selectNodes ("// tr"); 18 if (rows! = Null) 19 {20 int colCount = 0; 21 StringBuilder sbTable = new StringBuilder (); 22 foreach (HtmlAgilityPack. htmlNode td in rows [0]. childNodes. where (m => m. originalName. toLower () = "td") 23 {24 HtmlAgilityPack. htmlAttribute attr = td. attributes ["colspan"]; 25 int colspan = (attr! = Null )? Int. parse (attr. value): 1; 26 colCount = colCount + colspan; 27} 28 int rowCount = rows. count; 29 30 string [] [] arr = new string [rowCount] []; 31 for (int r = 0; r <rowCount; r ++) 32 {33 arr [r] = new string [colCount]; 34} 35 36 // fill Area 37 for (int r = 0; r <rowCount; r ++) 38 {39 HtmlAgilityPack. htmlNode tr = rows [r]; 40 List <HtmlAgilityPack. htmlNode> cols = tr. childNodes. where (m => m. origina1n Ame. toLower () = "td "). toList (); 41 42 int colspan = 0; 43 int rowspan = 0; 44 for (int c = 0; c <cols. count; c ++) 45 {46 HtmlAgilityPack. htmlAttribute cAttr = cols [c]. attributes ["colspan"]; 47 colspan = (cAttr! = Null )? Int. Parse (cAttr. Value): 1; 48 HtmlAgilityPack. HtmlAttribute rAttr = cols [c]. Attributes ["rowspan"]; 49 rowspan = (rAttr! = Null )? Int. parse (rAttr. value): 1; 50 string text = cols [c]. innerText. replace ("& nbsp ;",""). replace (",",","). replace ("\ r ",""). replace ("\ n ",""). trim (); 51 52 if (colspan = 1 & rowspan = 1) 53 {54 continue; 55} 56 57 bool isFirst = true; 58 int rFill = r + rowspan; 59 for (int ri = r; ri <rFill; ri ++) 60 {61 int cFill = c + colspan; 62 for (int ci = c; ci <cFill; ci ++) 63 {64 if (isFirst) 65 {66 text = (text = string. Empty )? "": Text; 67 arr [ri] [ci] = text; 68 isFirst = false; 69} 70 else 71 {72 arr [ri] [ci] = string. empty; 73} 74} 75} 76} 77} 78 79 // fill unit 80 for (int r = 0; r <rowCount; r ++) 81 {82 HtmlAgilityPack. htmlNode tr = rows [r]; 83 List <HtmlAgilityPack. htmlNode> cols = tr. childNodes. where (m => m. originalName. toLower () = "td "). toList (); 84 Queue <string> queue = new Queue <string> (); 85 for (int c = 0; C <cols. count; c ++) 86 {87 string text = cols [c]. innerText. replace ("& nbsp ;",""). replace (",",","). replace ("\ r ",""). replace ("\ n ",""). trim (); 88 queue. enqueue (text); 89} 90 for (int c = 0; c <colCount; c ++) 91 {92 if (arr [r] [c] = null) 93 {94 string text = queue. count> 0? Queue. Dequeue (): string. Empty; 95 arr [r] [c] = text; 96} 97 else 98 {99 if (arr [r] [c]! = String. empty) 100 {101 if (queue. count> 0) 102 {103 queue. dequeue (); 104} 105} 106} 107} 108 109 // assembled into cvs format content 110 foreach (string [] cols in arr) 112 {113 foreach (string col in cols) 114 {115 sbLines. append (col + ","); 116} 117 sbLines. appendLine (","); 118} 119 table. removeAll (); 120} 121} 122} 123 124 HtmlAgilityPack. htmlNodeCollection pList = doc. documentNode. selectNodes ("// p"); 125 if (pList! = Null) 126 {127 sbLines. appendLine ("# flag_text #,"); 128 foreach (HtmlAgilityPack. htmlNode p in pList) 129 {130 string text = p. innerText. replace ("& nbsp ;",""). replace (",",","). replace ("\ r ",""). replace ("\ n ",""). trim (); 131 text = GetTextByHtml (text); 132 if (! String. isNullOrWhiteSpace (text) 133 {134 sbLines. append (text + ","); 135 sbLines. appendLine (","); 136} 137 else138 {139 sbLines. appendLine (","); 140} 141 p. removeAll (); 142} 143} 144 145 HtmlAgilityPack. htmlNodeCollection dList = doc. documentNode. selectNodes ("// div"); 146 if (pList! = Null) 147 {148 sbLines. appendLine ("# flag_text #,"); 149 foreach (HtmlAgilityPack. htmlNode div in pList) 150 {151 string text = div. innerText. replace ("& nbsp ;",""). replace (",",","). replace ("\ r ",""). replace ("\ n ",""). trim (); 152 text = GetTextByHtml (text); 153 if (! String. isNullOrWhiteSpace (text) 154 {155 sbLines. append (text + ","); 156 sbLines. appendLine (","); 157} 158 else159 {160 sbLines. appendLine (","); 161} 162 // div. removeAll (); 163} 164} 165 return sbLines. toString (); 166}
Html:
Csv:
Url: http://www.cnblogs.com/dreamman/p/5343924.html