Challenge of Day

Monday, February 14, 2011

How to convert a string XML to data type XML in Visual Basic VB .NET

It is quite common to convert a file containing an XML content to XML data type and then other ADO .NET objects.
Here is a sample program:

dim x as XMLReader
dim s as String

x = XMLReader.create("c:\baridsoft\orders.xml")

Now you can play with object "x" and manipulate it using Readxxxx methods. But the question is that most of the times you have your XML in memory with a string presentation.

Dim sr As New System.IO.StringReader("ali khademi20")
Dim xx As New Xml.XmlTextReader(sr)
xx = XmlReader.Create(sr)

Here is a sample program that is supposed to convert string to XML but it will trap in exception:
Unable to cast object of type 'System.Xml.XmlTextReaderImpl' to type 'System.Xml.XmlTextReader'.

Here is the solution to this problem:

Dim sr As New StringReader("ali khademi20")
Dim xx As New Xml.XmlTextReader(sr)

Be a winner in your daily challenges
Ali Khademi

Labels: , ,