How to delete a file or folder in Powershell

To delete a file or Folder in Powershell you can use the following snippet.

Sample Powershell

if you want to delete a file use

remove-item -path "C:\Users\Codesnippets\Desktop\Test.xml" -force -recurse

if you want to delete a folder use

remove-item -path "C:\Users\Codesnippets\Desktop\Folder1" -force -recurse

for more informations see Using the Remove-Item Cmdlet

How to remove all children from XNode in C# and VB.NET

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

How to remove all children from XMLNode in C# and VB.NET

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