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

Sample C#

public static XDocument RemoveAllChildren(XNode xNode)
{
	if (xNode.IsNull() || String.IsNullOrEmpty(xNode.ToString()))
	{
		return null;
	}
	var xElement = xNode.ToXElement();
	xElement.RemoveNodes();
	return XDocument.Parse(xElement.ToString());
}

Sample VB.NET

Public Shared Function RemoveAllChildren(xNode As XNode) As XDocument
	If xNode.IsNull() OrElse [String].IsNullOrEmpty(xNode.ToString()) Then
		Return Nothing
	End If
	Dim xElement = xNode.ToXElement()
	xElement.RemoveNodes()
	Return XDocument.Parse(xElement.ToString())
End Function

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

Leave a Reply