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 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 validate XDocument with xsd file in C# and VB.NET

To validate xml file with xsd file in C# and VB.NET you can use the following snippet.

Sample C#

public string ValidateXDocument(XDocument xmlFile, string xsdFile)
{
	try
	{
		var retVal = String.Empty;
		var schemas = new XmlSchemaSet();
		schemas.Add(null, xsdFile);
		xmlFile.Validate(schemas, (o, e) =>
		{
			retVal = e.Message;
		});
		return retVal;
	}
	catch (Exception ex)
	{
		//handle the exception your way
		return ex.ToString();
	}
}

Sample VB.NET

Public Function ValidateXDocument(xmlFile As XDocument, xsdFile As String) As String
	Try
		Dim retVal = [String].Empty
		Dim schemas = New XmlSchemaSet()
		schemas.Add(Nothing, xsdFile)
		xmlFile.Validate(schemas, Function(o, e) 
		retVal = e.Message

End Function)
		Return retVal
	Catch ex As Exception
		'handle the exception your way
		Return ex.ToString()
	End Try
End Function

How to XSL transform a XDocument in C# and VB.NET

To XSL transform a XDocument in C# and VB.NET you can use the snippet below.
A more complex example which also works with custom namespaces can be found HERE.

Sample C#

public XDocument TransformXDocument(XDocument inputXml, string xslFile)
{
	try
	{
		var xslt = new XslCompiledTransform();
		var sb = new StringBuilder();
		using (var writer = XmlWriter.Create(sb))
		{
			xslt.Load(xslFile);
			xslt.Transform(inputXml.CreateReader(ReaderOptions.None), writer);
			writer.Close();
			writer.Flush();
		}
		return XDocument.Parse(sb.ToString());
		
	}
	catch (Exception ex)
	{
		//handle the exception your way
		return new XDocument();
	}
}

Sample VB.NET

Public Function TransformXDocument(inputXml As XDocument, xslFile As String) As XDocument
	Try
		Dim xslt = New XslCompiledTransform()
		Dim sb = New StringBuilder()
		Using writer = XmlWriter.Create(sb)
			xslt.Load(xslFile)
			xslt.Transform(inputXml.CreateReader(ReaderOptions.None), writer)
			writer.Close()
			writer.Flush()
		End Using

		Return XDocument.Parse(sb.ToString())
	Catch ex As Exception
		'handle the exception your way
		Return New XDocument()
	End Try
End Function

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

How to populate a XDocument from a String

How to populate a XDocument from a String?

Example C#

string str = "This is Child 1This is Child 2";
XDocument doc = XDocument.Parse(str);

Example VB.NET

Dim str As String = "This is Child 1This is Child 2"
Dim doc As XDocument = XDocument.Parse(str)

see the msdn for more informations XDocument.Parse Method