1112 ...">

Create a dictionary or list from a string (including an HTML tag) in C #

A has a line like this:

string s = @" <tr> <td>11</td><td>12</td> </tr> <tr> <td>21</td><td>22</td> </tr> <tr> <td>31</td><td>32</td> </tr>"; 

How to create a Dictionary<int, int> d = new Dictionary<int, int>(); from string s to get the same result as:

 d.Add(11, 12); d.Add(21, 22); d.Add(31, 32); 
+4
source share
6 answers

You must use the HTML Agility Pack .

For example: (verified)

 var doc = new HtmlDocument(); doc.LoadHtml(s); var dict = doc.DocumentNode.Descendants("tr") .ToDictionary( tr => int.Parse(tr.Descendants("td").First().InnerText), tr => int.Parse(tr.Descendants("td").Last().InnerText) ); 

If HTML is always well-formed, you can use LINQ-to-XML; the code will be almost identical.

+11
source

The code

 using RE=System.Text.RegularExpressions; .... public void Run() { string s=@ " <tr> <td>11</td><td>12</td> </tr> <tr> <td>21</td><td>22</td> </tr> <tr> <td>31</td><td>32</td> </tr>"; var mcol= RE.Regex.Matches(s,"<td>(\\d+)</td><td>(\\d+)</td>"); var d = new Dictionary<int, int>(); foreach(RE.Match match in mcol) d.Add(Int32.Parse(match.Groups[1].Value), Int32.Parse(match.Groups[2].Value)); foreach (var key in d.Keys) System.Console.WriteLine(" {0}={1}", key, d[key]); } 
+3
source
 string s = @"<tr> <td>11</td><td>12</td> </tr> <tr> <td>21</td><td>22</td> </tr> <tr> <td>31</td><td>32</td> </tr>"; XPathDocument doc = new XPathDocument(XmlReader.Create(new StringReader(s), new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment, IgnoreWhitespace = true })); Dictionary<int, int> dict = doc.CreateNavigator() .Select("tr") .Cast<XPathNavigator>() .ToDictionary( r => r.SelectSingleNode("td[1]").ValueAsInt, r => r.SelectSingleNode("td[2]").ValueAsInt ); 
+1
source

If you do not want to use the HTML flexibility package, you can try something similar to:

 var arr = s.Replace("<tr>", "").Split("</tr", StringSplitOptions.RemoveEmptyEntries); var d = new Dictionary<int, int>(); foreach (var row in arr) { var itm = row.Replace("<td>", "").Split("</td>", StringSplitOptions.RemoveEmptyEntries); d.Add(int.Parse(itm[0]), int.Parse(itm[1]); } 

(unverified)

0
source
 var s = "<tr><td>11</td><td>12</td></tr><tr><td>21</td><td>22</td></tr><tr><td>31</td><td>32</td></tr>"; var rows = s.Split( new[] { "</tr>" }, StringSplitOptions.None ); var results = new Dictionary<int, int>(); foreach ( var row in rows ) { var cols = row.Split( new[] { "</td>" }, StringSplitOptions.None ); var vals = new List<int>(); foreach ( var col in cols ) { var val = col.Replace( "<td>", string.Empty ).Replace( "<tr>", string.Empty ); int intVal; if ( int.TryParse( val, out intVal ) ) vals.Add( intVal ); } if ( vals.Count == 2 ) results.Add( vals[0], vals[1] ); } 
0
source

using RE = System.Text.RegularExpressions;

....

public void Run () {line s = @ "1112 2122 3132";

 var mcol= RE.Regex.Matches(s,"<td>(\\d+)</td><td>(\\d+)</td>"); var d = new Dictionary<int, int>(); foreach(RE.Match match in mcol) d.Add(Int32.Parse(match.Groups[1].Value), Int32.Parse(match.Groups[2].Value)); foreach (var key in d.Keys) System.Console.WriteLine(" {0}={1}", key, d[key]); 

}

0
source

Source: https://habr.com/ru/post/1303410/


All Articles