简体   繁体   中英

C# XMLDocument to DataTable?

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?

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.

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];
}

这样的事怎么样?

dsAPDP.ReadXml(new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xmlAPDP.OuterXml)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM