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

Please Share us:
Please rate this snippet

3 thoughts on “How to convert xml string to XmlNode in C# and VB.NET

Leave a Reply