To remove all children from XMLNode in C# and VB.NET you can use the follwing snippet.

Sample C#

public static XmlDocument RemoveAllChildren(XmlNode xmlNode)
{
	if (xmlNode.IsNull() || String.IsNullOrEmpty(xmlNode.ToString()))
	{
		return null;
	}
	while (xmlNode.HasChildNodes)
	{
		xmlNode.RemoveChild(xmlNode.FirstChild);
	}
	return xmlNode.OuterXml.ToXmlDocument();
}

Sample VB.NET

Public Shared Function RemoveAllChildren(xmlNode As XmlNode) As XmlDocument
	If xmlNode.IsNull() OrElse [String].IsNullOrEmpty(xmlNode.ToString()) Then
		Return Nothing
	End If
	While xmlNode.HasChildNodes
		xmlNode.RemoveChild(xmlNode.FirstChild)
	End While
	Return xmlNode.OuterXml.ToXmlDocument()
End Function

2 thought on “How to remove all children from XMLNode in C# and VB.NET”

Leave a Reply