How to use ReadXml? (direct read from a string xml)

Normally, we used the ReadXml to read a xml file.
but if I already have a xml in a string, ReadXml cannot directly read the xml into dataset/datatable!!!

The following example first creates a simple dataset by a xml string:

1. we has a xml string


sXml+="<root>"
sXml+=" <func>"
sXml+=" <function>Testing</function>"
sXml+=" </func>"
sXml+="</root>"



2. We convert the string into byte, and write into memory stream.

Dim myEncoder As New System.Text.ASCIIEncoding
Dim bytes As Byte() = myEncoder.GetBytes(sXml.ToString)
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(bytes)


3. We declare the dataset first.

Dim dsRoot As New DataSet("root")
Dim dtNew As DataTable = New DataTable("func")
dtNew.Columns.Add(New DataColumn("function", GetType(String)))
dsRoot.Tables.Add(dtNew)


4. Now, we can read the xml from the memory stream into the dataset.

dsRoot.ReadXml(ms)

0 comments: