views:

3960

answers:

4

I assume I have to do this via a DataSet, but it doesn't like my syntax.

I have an XMLDocument called "XmlDocument xmlAPDP".

I want it in a DataTable called "DataTable dtAPDP".

I also have a DataSet called "DataSet dsAPDP".

-

if I do DataSet dsAPDP.ReadXML(xmlAPDP) it doesn't like that because ReadXML wants a string, I assume a filename?

Thanks,

-Matt

A: 

How about something like this?

dsAPDP.ReadXml(new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xmlAPDP.OuterXml)))
Kon
+5  A: 

No hacks required:

xmlAPDP = new XmlDocument()
...
xmlReader = new XmlNodeReader(xmlAPDP)
dataSet = new DataSet()
...
dataSet.ReadXml(xmlReader)

XmlDocument is an XmlNode, and XmlNodeReader is a XmlReader, which ReadXml accepts.

Matthew Flaschen
Nice Matthew. Good job.
Boo
+2  A: 

Matthew : dont forget to close you XmlReader, or better use "using":

using (XmlReader reader = new XmlNodeReader(xmlAPDP.documentElement))
{
   dsAPDP.ReadXml(reader);
   reader.Close();
}
dtAPDP = dsAPDP.tables[0];
omadawn
+1  A: 

ASP.net example:

private DataTable GetReportDataTable()
{
    //get mapped path to xml document
    string xmlDocString = Server.MapPath("CustomReports.xml");

    //read into dataset
    DataSet dataSet = new DataSet();
    dataSet.ReadXml(xmlDocString);

    //return single table inside of dataset
    return dataSet.Tables[0];
}
Bob Yenser