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)
	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
	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