Difficulty arises when attempting to remove a node using Html Agility Pack

After successfully installing the Html Agility Pack to my asp.net project, I have been able to extract data from a different web page and display it without any issues. However, I am currently facing a problem. I have identified a div with the id of header, within which there is another div with the class name login that I want to remove. Below is the code snippet I am using:

//Get the div with the id of footer
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@id='header']");

//Remove login div
HtmlNode loginSection = node.SelectSingleNode("//div[@class='login']");

node.RemoveChild(loginSection);

Although the loginSection variable does contain a value at this point, indicating that the selected div exists, I encounter an error when trying to call the RemoveChild function:

Node "<div class="login"></div>" was not found in the collection

Despite searching for solutions on stackoverflow, I have yet to find a resolution to this issue.

Answer №1

After some investigation, I discovered that the key is to first select the parent node before accessing the loginSection node.

Here is the updated and functional code snippet:

// Identify the div element with the footer ID
HtmlNode parentNode = doc.DocumentNode.SelectSingleNode("//div[@id='header']");

// Locate the login section within the parent node
HtmlNode loginSection = parentNode.SelectSingleNode("//div[@class='login']");

// Remove the login section from its parent node
loginSection.ParentNode.RemoveChild(loginSection);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to stop Angular from automatically scrolling to the center of the page

I am facing an issue with my angular 11 application. Whenever I navigate to a specific page, there is an automatic scroll down which I don't want. Please refer to the attachment (gif) and homepage.html below to understand the scenario. https://i.ssta ...

Update the database with the information provided by this model using Ajax technology

There is an Ajax function in my View that I am having trouble with. function Save() { var Url = '@Url.Action("UpdateCaseDetails", "CaseDetailView")'; var frm = $("form"); var data = JSON.stringify(frm.serializeArray()); $.ajax({ ...

Troubleshooting Printing Issues on WordPress

After conducting a thorough search on Google to find solutions for this issue, I discovered that most of the fixes are specific to certain themes. While I did come across a suggestion to create a print.css file which partially solved the problem by printin ...

I want to switch images when the mouse hovers over the pager on Bxslider

My goal is to use mouse hover instead of clicking on the circles in bxSlider's pager, but I am unsure how to achieve this. I attempted changing the source code from click to hover, as demonstrated on a site like this one. The original code snippet lo ...

What is the most effective method for integrating Ajax file uploading?

Is it possible to incorporate an "ajax" file upload (is this concept commonly known by this name, or does it involve using an iframe for the file upload process?) specifically for uploading images within a form? For example, let's say a user wants to ...

"Exploring the possibilities of ASP.NET and NoSQL integration

After delving into some NoSQL concepts and reading articles about MongoDB & CouchDB, it appears that they offer a greater opportunity to build scalable web applications. However, I need more clarity on whether they provide the same features as SQL Server ...

Developing a Voting System in CodeIgniter

Currently, I am working on implementing a voting system in my CodeIgniter project called "like". I came across a method on and followed it. After successfully updating the database, I faced an issue where the like count was not displaying on the view. In ...

What sets SOAP and ASMX apart from each other?

When I set out to understand the relationship between SOAP webservices and ASMX webservice, I expected it to be simple. However, after researching online, I struggled to find a clear answer. Are SOAP webservices and ASMX webservices the same thing? Is ASMX ...

How to Add Pagination Feature in ASP.Net Gridview Component

I am facing an issue with my gridview that is bound on page load and I have implemented insert using the footer row technique. Recently, I tried adding pagination to the same gridview. Although there are no errors, when I change the page in the dropdown ...

Deciding Between Javascript DOM and Canvas for Mobile Game Development

My idea for a mobile game involves using the Phonegap framework to create a game where players can control a ball's movement by utilizing their phone's accelerometer. I also envision incorporating other elements like enemies and walls into the ga ...

My goal is to eliminate unnecessary code and transfer it into its own jQuery function

Currently, I am working on optimizing my code by removing redundancies and moving sections to separate functions. //Consolidating Infotypes for filtering and checking if any option is selected if(this.$infoOptions.val() != null){ ...

Utilizing jQuery to fetch the source value of an image when the closest radio button is selected

On my website, I have a collection of divs that display color swatches in thumbnail size images. What I want to achieve is updating the main product image when a user clicks on a radio button by fetching the source value of the image inside the label eleme ...

Conceal your password using HTML techniques

My goal is to hide passwords from being visible unless a user hovers directly over them. I've tried using the code below, but the hover effect isn't working as expected - either the password disappears completely or remains visible at all times. ...

Tips for converting a shiny R Markdown file into an HTML document

I've developed an RMD file that includes a chunk with data imported from an RDS file. I have utilized Shiny tools to make this data interactive. While the html displays perfectly in my browser, when I attempt to generate an html_document to share with ...

Reducing the size of XML files in ASP.NET 3.5 returned from WebMethods: Available solutions

Recently, I've come into possession of an ASP.NET web app built on .NET 3.5, along with its C# client components and a related JavaScript client. The server-side components currently use the default [WebMethod] serialization method, but due to the la ...

jQuery problem with setting and redirecting users if cookie value is missing

On my home page, there is a script that checks two things when a user visits our site: If the screen size is less than 800 pixels, they are redirected to the mobile site. If they have previously visited the mobile site and selected "View Full Site," ...

Angular - the offspring of another element

I'm currently exploring the possibilities of identifying if a clicked element is a child of another using Angular. In jQuery, I would typically use has() for this task, but I'm unsure of the equivalent method in Angular aside from iterating throu ...

Retrieve the content of each div element based on its text value

In this particular structure, I have: <table id="thetable"> <tr> <td> <div> <div>some text</div> <div> <div></div> ...

Tips for managing the response from a POST request using jQuery

I'm currently working on sending data via POST to my ASP.Net MVC Web API controller and retrieving it in the response. Below is the script I have for the post: $('#recordUser').click(function () { $.ajax({ type: 'POST', ...

Showing a restricted number of rows in the JSP page

<table class="grid_alt" cellspacing="0" rules="all" border="1" id="id1" style="width:720px;border-collapse:collapse;"> <tbody> <tr align="left"> <th scope="col"><%=partner %></th><th scope="col"><%=item %>< ...