ReadXML method

G

Guest

Hi All,

I have a xml file which is in a string variable(for example, sXML="<e1 at1=1
at2=2>aaa</e1>..."), how can I read it into dataset?

Thanks
 
G

Guest

William,

One way to do this is create a StringReader that reads the xml string then
create a XmlTextReader that will take the StringReader as an argument. At
this point you can read in the data by using the
DataSet.ReadXml(XmlTextReader) method. I have posted a sample for you below.

I hope this helps.
--------------------
StringBuilder sbXML = new StringBuilder();
sbXML.Append(@"<?xml version=""1.0""?><catalog><book
id=""bk112""><author>Galos, Mike</author><title>Visual Studio 7: A
Comprehensive Guide</title><genre>Computer</genre></book></catalog>");
DataSet ds = new DataSet();
StringReader sr = new StringReader(sbXML.ToString());
XmlTextReader xtr = new XmlTextReader(sr);
ds.ReadXml(xtr);
dataGrid1.DataSource = ds;
 
G

Guest

Hi Brian,
Thanks. It works.
I also tried another way which works too, I would like to share with other
people.
context = New System.Xml.XmlParserContext(Nothing, Nothing,
Nothing, System.Xml.XmlSpace.None)
reader = New System.Xml.XmlTextReader(sXML,
System.Xml.XmlNodeType.Document, context)
ds.ReadXml(reader)
 

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