6

How can I retrieve the value from a div tag via the ID using CSQuery?

For example,

<h3>
    <div id='type'>
        Room 1
    </div>
    <div id='price'>
        145
    </div>
</h3>

In this case I'd like to get the content inside type and price.

Sam
  • 7,252
  • 16
  • 46
  • 65
bluewonder
  • 767
  • 2
  • 10
  • 18
  • @ShashankChaturvedi : I don't know how to write a string to get the content, I tried dom.Select("[id = lib_presta").Text() but it didnt' work out – bluewonder Feb 28 '14 at 10:38

1 Answers1

8

Ok, here is how you do this with a full working example.

Html

This includes your invalid/duplicate id html which you have no control over

var html = @"<h3>
            <div id='lib_presta'>
                Chambre standard 1 pers du <span class=''>03/03/2014</span>  au <span class=''>05/03/2014 </span>
            </div>
            <div id='prix_presta'>
                127.76 &euro;
            </div>
        </h3><h3>
            <div id='lib_presta'>
                Chambre standard 2 pers du <span class=''>03/03/2014</span>  au <span class=''>05/03/2014 </span>
            </div>
            <div id='prix_presta'>
                227.76 &euro;
            </div>
        </h3>";

C# Code

This loads the dom elements by their id's into two lists of descriptions and prices. It then projects them into a list of HotelAvailability objects using the key values of both collections as the HotelName and Price properties.

        CQ dom = html;

        var libs = dom["#lib_presta"];
        var prixs = dom["#prix_presta"];

        var list = libs.Zip(prixs, (k, v) => new { k, v })
          .Select(h => new HotelAvailablity { HotelName = h.k.InnerText.Trim(), Price = h.v.InnerText.Trim() });

Screen grab

Run the above in a console app to test it.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • Thanks a lot, I could get it, but could you please guide me how to return those values then set it into the List and set to properties ? Can I use .ToList() in this case? Thanks – bluewonder Feb 28 '14 at 13:03
  • 1
    Sure no worries. If you want to get all divs without using id's you can simply do the following: var divList = dom["div"].ToList(); – hutchonoid Feb 28 '14 at 13:26
  • Yes, but I want to set the name and price as the properties and push them in the object class. now it just gets all the div values from html page, and I wish to have them separatedly as name and price. `List list = new List(); list.Add(new HotelAvailability() { HotelName = dom["#lib_presta"].Text(), Price = dom["#prix_presta"].Text() });` then It just gets the whole values into first element of List. you have any idea to split them into name and price? Thanks @hutchonoid – bluewonder Feb 28 '14 at 13:31
  • If I understand you correctly, it sounds like your dom is not correct. Are you returning several HotelNames from the following query? dom["#lib_presta"]. This should only be one as it is a unique id and the approach you mention above would be correct. – hutchonoid Feb 28 '14 at 13:42
  • 1
    If you are returning several you could add a price class and use a class selector (with a . instead of a #) to get the prices, then loop the list result and add it to you collection. – hutchonoid Feb 28 '14 at 13:43
  • Actually the hotel names are several, as I have some div tags which have same "id=lib_presta" indicating the name of the hotels, and the same for price, so after all, I got one big var including all the names of hotel and price. so I wish to split them out into each name in the Object HotelAvailability . Thanks @hutchonoid – bluewonder Feb 28 '14 at 14:00
  • Thanks @hutchonoid a lot for your help, but then I face another problem, as I don't develop the html page, I just parsed its content from the other side on internet, because of that, the html page cannot be modified in this case. I'm working on how to split them out after getting a variable of all hotel names. – bluewonder Feb 28 '14 at 15:33
  • 1
    @bluewonder No problem, I think you could take the same approach but using the id selector. I think it will return a collection still. Have you tried using var libs = dom["#lib_presta"]; var prixs = dom["#prix_presta"]; The using the dictionary method as above? – hutchonoid Feb 28 '14 at 15:37
  • Yes, but there is one problem for me, the `var libs = dom[".lib_presta"];` which returns a whole collection containing all the names getting from all the div tags id of lib_presta, the same for price. so that, for this step `var libs = new List() { "Desc 1", "Desc 2" };` I still dont get how to split them then assign to each property in the object List. Thanks @hutchonoid – bluewonder Feb 28 '14 at 15:56
  • @bluewonder Hi, I've added a full working example. Please upvote if it works for you. :) – hutchonoid Feb 28 '14 at 16:52
  • Thank you a lot for your help, it was a great example, I will test it now. have a nice day and thank you! – bluewonder Mar 04 '14 at 08:37