C#: HtmlAgilityPack Get the ID of selected node

I'm using HtmlAgilityPack to traverse some html  
Given the code I've created a dotnetfiddle

How do I get the number from the current div selected ID?
I'm extracting text from Red and navy fonts is this the best way to do it? and how Do I know what the exception is

I've not got this far yet but given an image url and a file path what is the best way to save an image
LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

Check my forked fiddle.

What i did was instead of using directly DescendantsAndSelf, i first got a SelectSingleNode and then enumerate upon its DescendantsAndSelf.
That allowed me to get the id of the divs. Then it was just a way of manipulating the value.

In a very simple example i just used a substring, but in a more real environment i think you should do more checks cause a simple substring could raise exceptions. For instance, what happens if the id is not of this exact pattern?

Well i guess this is covered anyway cause you only select the divs that have an id that match the pattern, so you should be ok with the substring as well.

Giannis
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Thank you

Can you tell me what the differance is between

         var DivHTML = htmlDoc.DocumentNode.DescendantsAndSelf("//div[starts-with(@id,'post_message_')]");
            foreach (var divNodes in DivHTML)

Open in new window


and Your
         var DivHTML = htmlDoc.DocumentNode.SelectSingleNode("//div[starts-with(@id,'post_message_')]");
            foreach (var divNodes in DivHTML.DescendantsAndSelf())

Open in new window


I thought SelectSingleNode would be used for a tag like "body"
0
trevor1940Author Commented:
I was trying  to  get the number from the div id using regular exspresion
                        Regex RegEXID = new Regex(@"post_message_(\d+)");
                        Match matchID = RegEXID.Match(div.Attributes["id"].Value);
                        if (matchID.Success)
                        {
                            var myID = matchID.Value;
                            Console.WriteLine("MyID = " + myID);
                        }
                      

Open in new window

I thought (\d+) matches just the number but I'm getting the whole ID
0
käµfm³d 👽Commented:
I thought (\d+) matches just the number but I'm getting the whole ID
It does, but you're confusing a match with a capture group. You put the numeric portion in a capture group, but in your C# you're not referring to said capture group. Instead:

var myID = matchID.Groups[1].Value;

Open in new window

0
trevor1940Author Commented:
Thanx for your help

I'd like to know the difference between the two selection methods for future reference

I didn't know about match groups
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.