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.

1
  • @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 Commented Feb 28, 2014 at 10:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks a lot, I could get it, but could you please guide me how to return those values then set it into the List<Object> and set to properties ? Can I use .ToList() in this case? Thanks
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();
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<HotelAvailability> list = new List<HotelAvailability>(); 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
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.
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.