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:

Please rate this snippet

How to convert a XMLDocument to XDocument in C# and VB.NET

To convert a XMLDocument to XDocument in C# and VB.NET you can use the following extensions method.

Sample C#

private XDocument ToXDocument(this XmlDocument xmlDocument)
{
    return XDocument.Parse(xmlDocument.OuterXml);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Private Function ToXDocument(xmlDocument As XmlDocument) As XDocument
	Return XDocument.Parse(xmlDocument.OuterXml)
End Function

for more informations see the MSDN: XmlDocument Class,XDocument-Class

Please rate this snippet