1

I want to help my problem is: I get the data but the data obtained in duplicate. Thank you. HTML

<div id="items" style="width: 940px; height: 2176px; position: relative;">
        <div class="item masonry-brick" style="top: 0px; right: 0px; position: absolute;">
        <div class="picture">
            <a title="bikini" class="image" href="...-bikini.html">
                <img alt="bikini" src="...13508.jpg">
            </a>
            <div class="item-content">
                <h2><a href="...bikini.html">bikini</a></h2>
                <div class="item_social">
                    <ul>
                        <li><i class="fa fa-eye"></i><span>6</span></li>
                        <li><i class="fa fa-thumbs-o-up"></i><span>0</span></li>
                        <li><i class="fa fa-comments"></i><span>0</span></li>
                    </ul>
                </div>
                <div class="author-post">
                    <a class="author" href="....nuong" rel="nofollow">
                        <img class="author_avatar" alt="nương" src="....ae3c3d8a6a.png">

                        <span class="author_name">nương</span>
                        <ul class="author_item">
                            <li><span>13 giờ trước </span></li>
                        </ul>
                    </a>
                </div>
            </div>
        </div>
    </div>
//..... more item masonry-brick
 </div>

My code C# parsing "but the data obtained in duplicate image and text!",but full item number.

HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlPage);
List<Data> datas = new List<Data>();
foreach (var div in htmlDocument.DocumentNode.SelectNodes("//div[starts-with(@class, 'item')]"))

{
    Data newdata = new Data();
    newdata.Imgsrc = div.SelectSingleNode("//div[@class='picture']//img").Attributes["src"].Value;
    newdata.Title = div.SelectSingleNode("//div[@class='item-content']//h2").InnerText.Trim();
    newdata.Summary = div.SelectSingleNode("//div[@class='author-post']//span").InnerText.Trim();
    datas.Add(newdata);
}
lstDatas.ItemsSource = datas;

Thanks you! Error!

1 Answer 1

1

You need to add period/dot (.) at the beginning of your XPath to indicate that the XPath searching scope is local within current div context :

foreach (var div in htmlDocument.DocumentNode.SelectNodes("//div[starts-with(@class, 'item')]"))
{
    Data newdata = new Data();
    newdata.Imgsrc = div.SelectSingleNode(".//div[@class='picture']//img").Attributes["src"].Value;
    newdata.Title = div.SelectSingleNode(".//div[@class='item-content']//h2").InnerText.Trim();
    newdata.Summary = div.SelectSingleNode(".//div[@class='author-post']//span").InnerText.Trim();
    datas.Add(newdata);
}

Otherwise, the XPath will search within entire HtmlDocument and return the first matched node again and again in every iteration, that's why you got those duplicates.

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

Comments

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.