Brian
Brian

Reputation: 558

How to Read XML into a DataSet

I have a class that goes to a URL and gets a xml document using xmlDoc.Load(URL). To test the class, I added a web project to display the xml in a grid view.

In a button click I create an instance of an xml document and populate it as:

xmlDoc = myClassName()

I'm stuck at how to get xmlDoc into a format usable by the datasource

I am totally confused as to how to get the xml to be displayed in the grid as dataset.ReadXml seems to want a file path. I don't understand the other overloads. I suppose I have to read the xml into a string or something else, but I don't understand how to do this - even after reading numerous posts here and MSDN - Thanks!

Upvotes: 2

Views: 23634

Answers (1)

Icarus
Icarus

Reputation: 63956

Example:

string xml =@"<xml><customer><id>1</id></customer></xml>";

DataSet ds = new DataSet();
ds.ReadXml(XmlReader.Create(new StringReader(xml)));

Now set the datasource to your grid:

grid.DataSource=newDataSet.Tables[0];

Update:

DataSet ds = new DataSet();
//xmlDocument is your XmlDocument instance
ds.ReadXml(XmlReader.Create(new StringReader(xmlDocument.InnerXml)));

grid.DataSource=newDataSet.Tables[0];

Upvotes: 15

Related Questions