<Security>
- <User>
  <Username>jon</Username> 
  <Password>khan</Password> 
  <UserFolder>aliHomeDIR</UserFolder> 
  </User>
- <User>
  <Username>bob</Username> 
  <Password>khan</Password> 
  <UserFolder>aliHomeDIR</UserFolder> 
  </User>
  </Security>

I have xml file and i want to remove specific element having username jon

foreach (XmlNode parentNode in parentNodes)
            {
                for (int i = 0; i <= parentNode.ChildNodes.Count - 1; i++)
                {
                    XmlNode childNode = parentNode.ChildNodes[i];
                    if (childNode.Name == "Username" && childNode.InnerXml == textBox1.Text.Trim())
                    {
                        childNode.ParentNode.RemoveAll();
                        xmlDoc.Save(filename);
                        //MessageBox.Show(childNode.InnerXml.ToString());

                    }
                }
            }

but using this code it removes inner part of <User> tag
<User>
</User>

instead of complete

<User>
<Username>jon</Username>
<Password>khan</Password>
<UserFolder>aliHomeDIR</UserFolder>
</User>

It looks like you may be off a node when you issue the delete.

Here's a console app that will do what you want - you can adapt the code as you see fit:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace XMLDelete
{
    class Program
    {
        static string sXML = "<?xml version=\"1.0\"?><Security><User><Username>jon</Username><Password>khan</Password><UserFolder>aliHomeDIR</UserFolder></User><User><Username>bob</Username><Password>khan</Password><UserFolder>aliHomeDIR</UserFolder></User></Security>";

        static void Main(string[] args)
        {
            XmlDocument myDoc = new XmlDocument();
            myDoc.LoadXml(sXML);
            XmlNode nodeToDelete = null;
            XmlNode topNode = myDoc.SelectSingleNode("/Security");
            foreach (XmlNode UserNode in topNode.ChildNodes)
            {
                foreach (XmlNode kid in UserNode.ChildNodes)
                {
                    if (kid.Name == "Username" && kid.InnerXml == "jon")
                    {
                        nodeToDelete = UserNode;
                        break;
                    }
                }
                
                if (nodeToDelete != null)
                    break;
            }

            if (nodeToDelete != null)
            {
                topNode.RemoveChild(nodeToDelete);
            }

            System.Console.WriteLine("Edited XML: " + myDoc.OuterXml);
        }
    }
}

mcriscolo thank you very much

Thats how you would do it with LINQ.

XDocument xdoc = XDocument.LoadXml(sXML);

XDocument xdoc = xdoc.Descendants("wtf").Where(p => p.Value == "on").FirstOrDefault().Parent.Remove();

how to use http connection in Asp.net.
Plase explain me.
I want to know stream post in Asp.net pages.

how to use http connection in Asp.net.
Plase explain me.
I want to know stream post in Asp.net pages.

You'd probably get more help in the ASP.NET forums under 'Web Development' for this question. :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.