Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello!

I would like to create one string a[4] from span class="c" and class="c2". a[0] = class="c"; a[1] = 0; a[2] = class="c2".
At this time my string contains only a[1] and a[2]. Please, help?

Here is an example of html code:


HTML
<div class="a">

<div class="b">
<span class="c">...
</span>
</div>


<div class="b1">...
</div>

<div class="b2">...
<span class="c">
</span>
</div>


</div>


What I have tried:

C#
var inner1 = doc.DocumentNode.SelectSingleNode("//div[@ class='a']");

var nodes3 = inner1.SelectNodes("//span[@ class='c']");

foreach (HtmlAgilityPack.HtmlNode item3 in nodes3)
                {
                    shipping_flag[i4] = item3.InnerText;
                   

                    shipping_array = (string[])shipping_flag.ToArray();
                    i4++;


                }
Posted
Updated 27-Jun-18 8:27am
v2

1 solution

I'll try to help you, however I'm a little puzzled about your explanation and your foreach block.
What do you like to achieve?
Extract all the SPAN elements into an array?
Count them?

EDIT:
Ok, i think I got it. I hope, at least :)
Just to be sure, are you like to get an output array like this:
C#
string[] shipping_array = new string[]
{
    "class=""c""",
    "0",
    "class=""c2""",
    null //why did not you define this item?
};

It seems to be a little inconsistent, however I don't know the backgrounds.
So, my next questions are:
* From where comes the '2' character in the third array element? It is a counter of found 'c' classes?
* Do you not defined the fourth array element. Why? There is any difference between short spans?

EDIT:
Try something similar:
C#
var results = new List<string>();
var divs = doc.DocumentNode.SelectNodes("//div");
foreach (var div in divs)
    results.Add(div.SelectNodes("span[@class='c']") == null ? "0" : "class=\"c\"");
 
Share this answer
 
v3
Comments
Member 12268183 27-Jun-18 15:02pm    
Thank you for your answer. I would like to extract all the span elements into an array. The second div has not a span. I would like the array to look like this: {span = "c", 0, span = "c"}, following the html code.
Member 12268183 27-Jun-18 15:55pm    
Yes, I would like to get an output array like the mentioned above from you. The '2' character in the third array element is a mistake. The third element doesn't have '2' character. I didn't define the fourth element. It's a mistake too. I apologize!
Member 12268183 27-Jun-18 15:58pm    
I would like to extract the spans from each div sequentially. When one div hasn't span element I would like to set this element to 0.
Liktor Janos 28-Jun-18 1:33am    
Oh, I almost forget: what about the external DIV? That has a span class="c" child element, althought not as direct descendants. What do you want with that DIV? Add as "class=\"c\"" to the array, or add as "0". Moreover, when do you wan to add it, on the opening or closing node?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900