C# XMLDocument to DataTable?

Matt Dell picture Matt Dell · May 7, 2009 · Viewed 51.7k times · Source

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?

Answer

Matthew Flaschen picture Matthew Flaschen · May 7, 2009

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.