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

  • SEARCH
  • Home
  • 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 515893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:40:30+00:00 2026-05-13T07:40:30+00:00

well i have the following problem. the html i have is malformed and i

  • 0

well i have the following problem.
the html i have is malformed and i have problems with selecting nodes using html agility pack when this is the case.
the code is below:

string strHtml = @"
<html>
  <div>
    <p><strong>Elem_A</strong>String_A1_2 String_A1_2</p>
    <p><strong>Elem_B</strong>String_B1_2 String_B1_2</p>
  </div>
  <div>
    <p><strong>Elem_A</strong>String_A2_2 <String_A2_2> asdas</p>
    <p><strong>Elem_B</strong>String_B2_2 String_B2_2</p>
  </div>
</html>";
HtmlAgilityPack.HtmlDocument objHtmlDocument = new HtmlAgilityPack.HtmlDocument();
objHtmlDocument.LoadHtml(strHtml);
HtmlAgilityPack.HtmlNodeCollection colnodePs = objHtmlDocument.DocumentNode.SelectNodes("//p");
List<string> lststrText = new List<string>();
foreach (HtmlAgilityPack.HtmlNode nodeP in colnodePs)
{
  lststrText.Add(nodeP.InnerHtml);
}

the problem is that String_A2_2 is enclosed in brackets.
so htmlagility pack returns 5 strings instead of 4 in the lststrText.
so is it possible to let htmlagility pack return element 3 as
"<strong>Elem_A</strong>String_A2_2 <String_A2_2> asdas"?
or maybe i can do some preprocessing to close the element?
the current content of lststrText is

lststrText[0] = "<strong>Elem_A</strong>String_A1_2 String_A1_2"  
lststrText[1] = "<strong>Elem_B</strong>String_B1_2 String_B1_2"  
lststrText[2] = ""  
lststrText[3] = ""  
lststrText[4] = "<strong>Elem_B</strong>String_B2_2 String_B2_2"
  • 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-13T07:40:31+00:00Added an answer on May 13, 2026 at 7:40 am

    Most html parsers try to build a working DOM, meaning dangling tags are not accepted. They will be converted, or closed in some way.

    If only selecting the nodes is of importance to you, and speed and huge amounts of data is not an issue, you could grab all your <p> tags with a regular expression instead:

    Regex reMatchP = new Regex(@"<(p)>.*?</\1>");
    foreach (Match m in reMatchP.Matches(strHtml))
    {
       Console.WriteLine(m.Value);
    }
    

    This regular expression assumes the <p> tags are well formed and closed.

    If you are to run this Regex a lot in your program you should declare it as:

    static Regex reMatchP = new Regex(@"<(p)>.*?</\1>", RegexOptions.Compiled);
    

    [Edit: Agility pack change]

    If you want to use HtmlAgility pack you can modify the PushNodeEnd function in HtmlDocument.cs:

    if (HtmlNode.IsCDataElement(CurrentNodeName()))
    {
       _state = ParseState.PcData;
       return true;
    }
    
    // new code start
    if ( !AllowedTags.Contains(_currentnode.Name) )
    {
        close = true;
    }
    // new code end
    

    where AllowedTags would be a list of all known tags: b, p, br, span, div, etc.

    the output is not 100% what you want, but maybe close enough?

    <strong>Elem_A</strong>String_A1_2 String_A1_2
    <strong>Elem_B</strong>String_B1_2 String_B1_2
    <strong>Elem_A</strong>String_A2_2 <ignorestring_a2_2></ignorestring_a2_2> asdas
    <strong>Elem_B</strong>String_B2_2 String_B2_2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposed I have the following code: <html:link onclick=jQuery('#add').data('name','${name}').dialog('open'); href=#> And with this, if ${name}
Well i have the following code: <?php while(1==1){ echopiece<br>; flush(); }; ?> The problem
I have the following problem that the standard library doesn't solve well, and I'm
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
Well, I have the following class definitions: private ObservableCollection<Node> _Nodes; public ObservableCollection<Node> Nodes {
well I have that problem, im using a lightbox srcipt and im opening an
I have the following problem. Developing APPs on IOS using Flex 4.6 I am
Well I have a simple problem with Nokogiri. I want to make Nokogiri::HTML::Builder to
I have some problems to run the following code, the js: $(document).ready(function(){ $.validator.addMethod(name, function(value,
My problem is a little bit complex. I have the following code: $(document).ready(function() {

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.