Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 334039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:01:03+00:00 2026-05-12T10:01:03+00:00

I’m using .NET 3.5 (C#) and the HTML Agility Pack to do some web

  • 0

I’m using .NET 3.5 (C#) and the HTML Agility Pack to do some web scraping. Some fields that I need to extract are structured as paragraphs within which components are separated by line-break tags. I’d like to be able to select out the individual components between the line-breaks. Each component may be formed from multiple elements (i.e., it may not be just a single string). Example:

<h3>Section title</h3>
<p>
  <b>Component A</b><br />
  Component B <i>includes</i> <strong>multiple elements</strong><br />
  Component C
</p>

I’d like to select out

<b>Component A</b>

Then:

Component B <i>includes</i> <strong>multiple elements</strong>

And then:

Component C

There may be more (<br /> separated) components, too.

I can easily get the first component with:

p/br[1]/preceding-sibling::node()

I can also easily get the last component with:

p/br[2]/following-sibling::node()

But I haven’t been able to work out how to extract the set of nodes /between/ two other tags (that is, nodes which are siblings but which precede node X and follow node Y).

The alternative is to scan through the elements manually – if that’s the easiest way to do it then that’s what I’ll do, but XPath has so-far impressed me with its terseness, so I’m hoping there’s a way of doing this, too.

Edit

Since I need to cope with the situation of having more than 3 components, it seems the answer will require multiple XPath calls at a minimum, so I shall proceed with a solution based on that (this is the answer I have ‘accepted’). AakashM’s answer has also helped me with my understanding of XPath, and so I have voted it up.

Thank you all for your help! I hope I can return the favour one day.

Edit 2

The new answer provided by Dimitre Novatchev, with some tweaks, does indeed work correctly.

Solution:

int i = 0;
do
{
    yield return para.SelectNodes(String.Format(
        "node()[not(self::br) and count(preceding-sibling::br) = {0}]", i));
    ++i;
} while (para.SelectSingleNode(String.Format("br[{0}]", i)) != null);

I should note that this loop is somewhat inefficient due to the repeated XPath queries to find out whether there are any more br tags. In my case that inefficiency is not a problem, but be aware if you want to use this answer in some other situation (then again, if you did want to do this in a performance sensitive situation you should probably be scanning through manually anyway rather than using XPath).

And full test code (a modified version of the test code helpfully included by AakashM):

using System;
using System.Collections.Generic;
using System.Xml;

namespace TestXPath
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"
<x>
 <h3>Section title</h3>
 <p>
  <b>Component A</b><br />
  Component B <i>includes</i> multiple <strong>elements</strong><br />
  Component C
 </p>
</x>
            ");


            foreach (var nodes in SplitOnLineBreak(doc.SelectSingleNode("x/p")))
            {
                Dump(nodes);
                Console.WriteLine();
            }

            Console.ReadLine();
        }

        private static IEnumerable<XmlNodeList> SplitOnLineBreak(XmlNode para)
        {
            int i = 0;
            do
            {
                yield return para.SelectNodes(String.Format(
                    "node()[not(self::br) and count(preceding-sibling::br) = {0}]", i));
                ++i;
            } while (para.SelectSingleNode(String.Format("br[{0}]", i)) != null);
        }

        private static void Dump(XmlNodeList nodes)
        {
            foreach (XmlNode node in nodes)
            {
                Console.WriteLine(string.Format("-->{0}<---", 
                                  node.OuterXml));                    
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:01:03+00:00Added an answer on May 12, 2026 at 10:01 am

    This can easily be done with XPath 2.0 or with XPath 1.0 hosted by XSLT.

    With XPath 1.0 hosted by .NET this can be achieved in several steps:

    1. Make the appropriate “p” node the current node.

    2. Find the number of all <br /> children of the current “p” node:

      count(br)

    3. if N is the count, determined in step 2. for $k in 0 to N do:

      3.1 Find all nodes that are preceded by $k <br /> elements:

      node()[not(self::br) and count(preceding::br) = $k]

      3.2 For every such node found, get its string value

      3.3 Concatenate all string values obtained in step 3.2. The result of this concatenation is all the text contained in the given paragraph.

    Note: In order to substitute what should stand for $k in step 3.1 it is necessary to dynamically construct this expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 172k
  • Answers 172k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Dim FT = (From T In _dataContext.Topic.Include("Replies").Include("Forum") _ Where T.TOPIC_ID… May 12, 2026 at 2:30 pm
  • Editorial Team
    Editorial Team added an answer There is a limit to the amount of rows the… May 12, 2026 at 2:30 pm
  • Editorial Team
    Editorial Team added an answer Just put your scripts at the end of the <body>,… May 12, 2026 at 2:30 pm

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.