2

I am using this to get some data:

string s = dom["table .lotto strong"].Text();

But I get everything in 1 line. For can I parse each element from that table separately and manipulate it (for example add <br /> after each element, so I don't get data in 1 line).

Documentation surprisingly doesn't show the iteration.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
sensei
  • 7,044
  • 10
  • 57
  • 125
  • do you want html multi-line or just enumerate all the lines (`\r\n`) in `s`? – Stefan Nov 11 '13 at 12:27
  • i will put that text that i get, into my table and show in html view in mvc. the source from their website is just usual table-td thing, but i don't know syntax with csquery. – sensei Nov 11 '13 at 12:29

2 Answers2

5

If you will look at description of Text function, it returns just a string. Basically if you have few elements for example 2 div, it will add everything into 1 string. (Like string.Join())

If I got what you want so it's something like that =>

dom["table .lotto strong"].Each(dom=> {
   //doing something what you need with dom var (dom is a IDomObject type)
});
nazarkin659
  • 503
  • 3
  • 11
  • See also https://github.com/jamietre/CsQuery, scroll down to Each, for syntax with the `i,e` arguments. – goodeye Jun 22 '16 at 00:08
3

Haj. I managed to to it. Example:

           CQ dom = CQ.CreateFromUrl("http://www.somewebsite.xxx");

        var rows = dom.Select(".box_results table.results-box_list tr");

        var output = @"<table class='table tableballs'>
                        <thead>
                          <tr>
                            <th></th>
                            <th>Syndicate</th>
                            <th>Draw Date</th>
                            <th>Results</th>
                          </tr>
                        </thead>

                        <tbody>";

        foreach (var row in rows.Has("td"))
        {
            output += "<tr>";
            CQ tdcells = row.Cq().Find("td");

            output += String.Format("<td></td><td>{0}</td><td>{1}</td>",
                tdcells[0].Cq().Text(),
                tdcells[1].Cq().Text());

            var uls = dom.Select(".ball_numbers ul");
            int count = 0;

            output += "<td><ul class='balls'>";
            var numbersTrim = "";

            foreach (var ul in tdcells[2].Cq())
            {
                CQ licells = ul.Cq().Find("li");

                foreach (var li in licells)
                {
                    numbersTrim += String.Join("", "<li>" + licells[count].Cq().Text() + "</li>");
                    count++;
                }
                output += numbersTrim;
            }
            count = 0;
            output += @"</ul></td>
                       </tr>";
        }

        output += @"</table>";

enjoy old buddies old pals

sensei
  • 7,044
  • 10
  • 57
  • 125