0

I have a code in C# where I want to extract the below value (the text "I want this text" in the HTML code below). I have reformat the HTML code to make it easily readable.

<div class="paste-copy-url" style="margin:0 0 0 0;">
    <h4>My Stats:</h4>
    <div class="line">
        <div class="wrap-input">
            <input onclick="this.select();" value="I want this text" readonly="readonly">
        </div>
    </div>
    <h4>Website Link:</h4>
    <div class="line">
        <div class="wrap-input"><input onclick="this.select();" value="Some value" readonly="readonly">
        </div>
    </div>
</div>

The code I tried (It is giving me the text : "Website Link:"):

var myvaluetoextract = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='paste-copy-url']");

What am I doing wrong? Can I use this approach to get that element (There is only 1 instance of the div class in the page)?

4
  • 2
    did you mean that myvaluetoextract is null...The above xpath is correct and should work.. Commented Oct 7, 2013 at 9:14
  • It is giving me the text : "Website Link:" (the header instead of the value I want) Commented Oct 7, 2013 at 9:22
  • @touyets you said returns a null value,now say It is giving me the text : "Website Link:". Which one is correct? Commented Oct 7, 2013 at 9:27
  • I edited my post. The correct one is this result after I corrected the page's url: It is giving me the text : "Website Link:" Commented Oct 7, 2013 at 9:32

2 Answers 2

4
var input = htmlDocument.DocumentNode
           .SelectSingleNode("//div[@class='paste-copy-url']//div[@class='wrap-input']/input");
var yourText = input.Attributes["value"].Value;
Sign up to request clarification or add additional context in comments.

2 Comments

it gives me the value "Some value" which is the value for the second element, not the first which is the one i want.
@touyets No it returns I want this text given your sample html in the question.
0

You can do it like this:

var myvaluetoextract = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='paste-copy-url']//input");
var value = myvaluetoextract.GetAttributeValue("value", null);

//input means you search for input elements in the div's subtree, recursively. GetAttributeValue is a helper that will never fail, even if the attribute doesn't exists (in this case if will return the 2nd passed parameter - which is null here)

2 Comments

it gives me the value "Some value" which is the value for the second element, not the first which is the one i want.
Then your extract does not match your real code. My code works with the html in your question.

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.