How to use a specific encoding using XDocument in C# and VB.NET

To use a specific encoding when working with XDocument, you can create an XDeclaration.

Samples

C# Sample

VB.NET Sample

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

How to get all types that implement a specific Interface in C# and VB.NET

To retrieve a IEnuemerable of all types in the current or another Assembly that implement a specific Interface / Abstract class and so on, you can use the snippets below.

Methods

C# Version

VB.NET Version

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

Do you have an alternate or better method for this task?
If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

How to check if a Form is Open in C# and VB.NET

To Check if a Form is open in C# and VB.NET you can use the snippet below.

Sample C#

public static bool FormIsOpen(FormCollection application, Type formType)
{
	//usage sample: FormIsOpen(Application.OpenForms,typeof(Form2)
	return Application.OpenForms.Cast<Form>().Any(openForm => openForm.GetType() == formType);
}

Sample VB.NET

Public Shared Function FormIsOpen(application__1 As FormCollection, formType As Type) As Boolean
	'usage sample: FormIsOpen(Application.OpenForms,GetType(Form2)
	Return Application.OpenForms.Cast(Of Form)().Any(Function(openForm) openForm.[GetType]() = formType)
End Function

How to get XNode as Xml String with indentation in C# and VB.NET

Sample C#

public static String XNodeToString(XNode node, int indentation = 2)
{
	using (var sw = new StringWriter())
	{
		using (var xw = new XmlTextWriter(sw))
		{
			xw.Formatting = Formatting.Indented;
			xw.Indentation = indentation;
			node.WriteTo(xw);
		}
		return sw.ToString();
	}
}

Sample VB.NET

Public Shared Function XNodeToString(node As XNode, Optional indentation As Integer = 2) As String
	Using sw = New StringWriter()
		Using xw = New XmlTextWriter(sw)
			xw.Formatting = Formatting.Indented
			xw.Indentation = indentation
			node.WriteTo(xw)
		End Using
		Return sw.ToString()
	End Using
End Function

for more informations on indentation = 2 see XmlTextWriter.Indentation Property

How to get XmlNode as Xml String with indentation in C# and VB.NET

Sample C#

public static String XmlNodeToString(XmlNode node, int indentation = 2)
{
	using (var sw = new StringWriter())
	{
		using (var xw = new XmlTextWriter(sw))
		{
			xw.Formatting = Formatting.Indented;
			xw.Indentation = indentation;
			node.WriteContentTo(xw);
		}
		return sw.ToString();
	}
}

Sample VB.NET

Public Shared Function XmlNodeToString(node As XmlNode, Optional indentation As Integer = 2) As String
	Using sw = New StringWriter()
		Using xw = New XmlTextWriter(sw)
			xw.Formatting = Formatting.Indented
			xw.Indentation = indentation
			node.WriteContentTo(xw)
		End Using
		Return sw.ToString()
	End Using
End Function

for more informations on indentation = 2 see XmlTextWriter.Indentation Property

How to convert xml string to XmlNode in C# and VB.NET

To convert xml string to XmlNode in C# and VB.NET you can use the following snippet.

Sample C#

public static XmlNode XmlStringToXmlNode(string xmlInputString)
{
	if (String.IsNullOrEmpty(xmlInputString.Trim())) { throw new ArgumentNullException("xmlInputString"); }
	var xd = new XmlDocument();
	using (var sr = new StringReader(xmlInputString))
	{
		xd.Load(sr);
	}
	return xd;
}

Sample VB.NET

Public Shared Function XmlStringToXmlNode(xmlInputString As String) As XmlNode
	If String.IsNullOrEmpty(xmlInputString.Trim()) Then
		Throw New ArgumentNullException("xmlInputString")
	End If
	Dim xd = New XmlDocument()
	Using sr = New StringReader(xmlInputString)
		xd.Load(sr)
	End Using
	Return xd
End Function

How to convert xml string to XNode in C# and VB.NET

To convert xml string to XNode in C# and VB.NET you can use the snippet below.

Sample C#

public static XNode XmlStringToXNode(string xmlInputString)
{
	if (String.IsNullOrEmpty(xmlInputString.Trim())) { throw new ArgumentNullException("xmlInputString"); }
	using (var stringReader = new StringReader(xmlInputString))
	{
		using (var xmlReader = XmlReader.Create(stringReader))
		{
			xmlReader.Read();
			return XNode.ReadFrom(xmlReader);
		}
	}
}

Sample VB.NET

Public Shared Function XmlStringToXNode(xmlInputString As String) As XNode
	If String.IsNullOrEmpty(xmlInputString.Trim()) Then
		Throw New ArgumentNullException("xmlInputString")
	End If
	Using stringReader = New StringReader(xmlInputString)
		Using xmlReader__1 = XmlReader.Create(stringReader)
			xmlReader__1.Read()
			Return XNode.ReadFrom(xmlReader__1)
		End Using
	End Using
End Function

How to query a DataTable using LINQ in C# and VB.NET

To query a DataTable using LINQ in C# and VB.NET you can use the snippet below.
In order to use the snippet, you need to reference System.Data.DataSetExtensions.

Sample C#

var resultData = (from myDataRow in myDataTable.AsEnumerable() where myDataRow.Field<Int32>("Fieldname") == 1 select myRow);

Sample VB.NET

Dim resultData = (From myDataRow In myDataTable.AsEnumerable() Where myDataRow.Field(Of Int32)("Fieldname") = 1myRow)

How to convert XNode to XElement in C# and VB.NET

To convert XNode to XElement in C# and VB.NET you can use the following snippet.

Samples

Sample C#

Sample VB.NET

 

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

 

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