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