How to read XML into a DataTable?

后端 未结 7 1596
星月不相逢
星月不相逢 2020-12-16 02:51

I have some XML in a string in memory exactly like this:


  EURCHF
  EURGBP         


        
7条回答
  •  执念已碎
    2020-12-16 03:32

    Dynamic Xml to DataTable

    public DataTable XMLToDataTable(string YourFilePath)
    {
            DataTable table = new DataTable("XMLTABLE");
            try
            {
                #region "'< <> >& NOT VALID EXTENSTION IN XML
                var xmlContent = File.ReadAllText(YourFilePath);
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlContent.Replace("'", "'").Replace("&", "&"));
                xDoc.Save(YourFilePath);
                #endregion
                //All XML Document Content
                //XmlElement root = xDoc.DocumentElement;
                string RootNode = xDoc.DocumentElement.Name;
                string RootChildNode = xDoc.DocumentElement.LastChild.Name;
                DataSet lstNode = new DataSet();
                lstNode.ReadXml(YourFilePath);
                table = lstNode.Tables[RootChildNode];
                return table;
            }
            catch (Exception ex)
            {
                
            }
    }
    

提交回复
热议问题