Read xml into Dataset in Compact Framework

G

Guest

What i need to do is read an xml string into a new dataset. In the full
framework, this is simply:

System.IO.StringReader sr = new System.IO.StringReader(xml);
DataSet ds = new DataSet();
ds.ReadXml(sr);

However, in the CF, the DataSet.ReadXml method wants an XmlTextReader, or a
filename, and i can't get either to work.

I have tried:

System.IO.StringReader sr = new System.IO.StringReader(xml);
DataSet ds = new DataSet();
XmlTextReader xmlr = new XmlTextReader( sr );
ds.ReadXmlSchema( xmlr );
ds.ReadXml( xmlr );

But get an XmlException on the ReadXmlSchema call.

The only examples i've found online so far involve reading the xml in from a
file.

Can anyone offer any suggestions?

Thanks.
 
T

Tim De Vogel

Hi Ian,

Afaik,
Dim ds As New DataSet()
ds.ReadXml("\directory\file.xml")

should do the trick.
 
G

Guest

Tim,

Thanks, but I need to read the xml from a string in memory, and I don't want
to have to write it out to a file just to read it in again. I think from
what I've seen that the TextReader is the way to go, but i just can't get it
working.
 
T

Tim De Vogel

If you have VS2005, there is a snippet that does this. It produces
following code:

Dim reader As New XmlTextReader("Snippet.xml")
Dim contents As String = ""
Dim indent As Integer = 0
While reader.Read()
reader.MoveToContent()
If reader.NodeType = XmlNodeType.Element Then
contents &= StrDup(indent, vbTab) & reader.Name & ": " & vbCrLf
indent += 1
End If
If reader.NodeType = XmlNodeType.Text Then
contents &= StrDup(indent, vbTab) & reader.Value & vbCrLf
End If
If reader.NodeType = XmlNodeType.EndElement Then
indent -= 1
End If
End While
MsgBox(contents)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top