0
votes

I have the following HTML: This is the html file in text format I am reading this from Local Hard disk:

 "<span style=""font-size:14px;""><span style=""""><strong>Description:</strong><br />
  Material:Cotton+Polyester<br />
  Color:White-Black<br />
  Occasion: Casual<br /><br />
<strong>Details&nbsp;in&nbsp;size:</strong></span></span><br />

<div border=""1"" class=""tab02"" style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; text-align: center; font-size: 14px; font-family: Arial- Helvetica- sans-serif;"" width=""100%"">
<div>
    <div style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px;"">
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            US Size</span>
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            M</span>
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            L</span>
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            XL</span>
    </div>
    <div style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px;"">
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            Asian&nbsp;Size</span>
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            L</span>
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            XL</span>
        <span style=""border: 1px dashed rgb(204- 204- 204); border-collapse: collapse; border-spacing: 0px; padding: 5px 10px;"">
            2XL</span>
    </div>

I need to get the innerDiv using C# and Xpath. This is What I have done so far: I am usign Xpath and

string SizeDescriptions = File.ReadAllText(@"E:\Elance\Product Description     HTML\HTML_Product_Description.txt");
        HtmlDocument document = new HtmlDocument();
        string htmlString = SizeDescriptions;// "<html>blabla</html>";
        document.LoadHtml(htmlString);
        HtmlNodeCollection collection = document.DocumentNode.SelectNodes("//div").FindFirst("div").ChildNodes;
        foreach (HtmlNode link in collection)
        {
            HtmlNodeCollection Sizes = link.SelectNodes("/div/span");
            foreach(HtmlNode SizeDiv in Sizes)
            {
                TableRow tr1 = new TableRow();
                TableCell cell1 = new TableCell();
                tr1.


            }
            string target = link.Attributes["href"].Value;
        }
1
Which div is innerDiv? is it class or id? - Uriil
HTML cannot begin with a quote sign. - AgentFire
@AgentFire HTML can begin with anything and can contain any combination of non-balnced quotes, tags. Valid HTML (can't be found in a wild, there are species in national zoos:) ) indeed can't start with quote. - Alexei Levenkov
"//div" selects all div elements in whole document... If you want help you may want to describe what node you need to select... - Alexei Levenkov
@Uriil need to get innerText of span elements in the lower most two div's (it could be more) - Dot_NET Pro

1 Answers

0
votes

Use

HtmlNodeCollection innerDivs = document.DocumentNode.SelectNodes("//div/div");
foreach (HtmlNode div in innerDivs)
{
    HtmlNodeCollection spans = link.SelectNodes("span");
    foreach(HtmlNode span in spans)
    {
        string text = span.InnerText;


    }

}

Of course if it doesn't matter which div a span belongs to then simply use one XPath and foreach e.g.

HtmlNodeCollection spans = document.DocumentNode.SelectNodes("//div/div/span");

foreach(HtmlNode span in spans)
{
    string text = span.InnerText;


}