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 8730093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:57:02+00:00 2026-06-13T08:57:02+00:00

I am loading the below xml using LoadXml. I need to remove the entire

  • 0

I am loading the below xml using LoadXml. I need to remove the entire <site> .... </site> node based on a condition using C#.

Followed:

  XmlDocument xdoc= new XmlDocument(); 
  xdoc.LoadXml(xmlpath); 
  string xml = xdoc.InnerXml.ToString();

  if(xml.Contains("href"+"\""+ "www.google.com" +"\"")
  {
  string removenode = "";  // If href=www.google.com is present in xml then remove the  entire node. Here providing the entire <site> .. </site>
  xml.Replace(removenode,"");
  }

It is not replacing the node with null

The XML is:

 <websites>
 <site>
 <a xmlns="http://www.w3.org/1999/xhtml" href="www.google.com"> Google </a>
 </site>
 <site>
 <a xmlns="http://www.w3.org/1999/xhtml" href="www.hotmail.com"> Hotmail </a>
 </site>
 </websites>
  • 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-06-13T08:57:03+00:00Added an answer on June 13, 2026 at 8:57 am

    Here’s an example that removes the site element containing any element with an href attribute containing www.google.com

    using System.Diagnostics;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string frag = @" <websites>
     <site>
     <a xmlns=""http://www.w3.org/1999/xhtml"" href=""www.google.com""> Google </a>
     </site>
     <site>
     <a xmlns=""http://www.w3.org/1999/xhtml"" href=""www.hotmail.com""> Hotmail </a>
     </site>
     </websites>";
    
                var doc = XDocument.Parse(frag);
    
                //Locate all the elements that contain the attribute you're looking for
                var invalidEntries = doc.Document.Descendants().Where(x =>
                {
                    //Get the href attribute from the element
                    var hrefAttribute = x.Attribute("href");
                    //Check to see if the attribute existed, and, if it did, if it has the value you're looking for
                    return hrefAttribute != null && hrefAttribute.Value.Contains("www.google.com");
                });
    
                //Find the site elements that are the parents of the elements that contain bad entries
                var toRemove = invalidEntries.Select(x => x.Ancestors("site").First()).ToList();
    
                //For each of the site elements that should be removed, remove them
                foreach(var entry in toRemove)
                {
                    entry.Remove();
                }
    
                Debugger.Break();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the below code to use xml loading in jsfiddle.net $.ajax({ type:
I'm using this script below for loading a hidden div, inside another: $(document).ready(function(){ $(function()
I am using asp.net when loading image it gives me error as shown below.
I have a UITableView which loads data from XML using the below code. It
I am using Libxml2 to read/write XML files. For catching libxml errors while loading
I am using the below code to read one xml file which is located
I am using C# 2.0 and I have got below code: XmlDocument doc =
I have XML as below which I am loading into XDocument <ConversionSpecification xmlns=http://tempuri.org/X12ParserSpecification.xsd TransactionSetIdentifierCode=837>
I am getting the below mentioned error while loading the JSF page. The page
I'm loading Json into an NSDictionary and the output is below. I'm just not

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.