• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 67
  • Last Modified:

Need help with removing nodes from XmlDocument object in C#

How can I remove an xml node from an XmlDocument object in c#?  For example, I want to remove <LastName> tag.....


Input:
<DataSet>
	<Person>
		<FirstName>Bob</FirstName>
		<LastName>Smith</LastName>
	</Person>
	<Person>
		<FirstName>Gina</FirstName>
		<LastName>Davis</LastName>
	</Person>
</DataSet>

Open in new window


Desired Result:

<DataSet>
	<Person>
		<FirstName>Bob</FirstName>
	</Person>
	<Person>
		<FirstName>Gina</FirstName>
	</Person>
</DataSet>

Open in new window



I'm using .NET Framework 3.5
0
silentthread2k
Asked:
silentthread2k
2 Solutions
 
Andreas HaasCommented:
XmlNodeList nodes = xdoc.GetElementsByTagName("LastName");
for (int i = 0; i < nodes.Count; i++)
{
    XmlNode node = nodes[i];
    if (node.ParentNode.Name.Equals("Person"))
    {
        node.ParentNode.RemoveChild(node);
        i--;
    }
}

Open in new window

0
 
leakim971PluritechnicianCommented:
What about :
xmlDoc.RemoveChild("LastName");

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now