A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


Process XML to DataTable Function

 

This code can be used to use loop through xml , grab a specifc set of nodes and dump them to  data table. 

        public static void processMyXml(string xmlpath, DataTable dt)
        {

            XmlTextReader reader = new XmlTextReader(xmlpath);

            try
            {
                DataRow dr;
                while (reader.Read())
                {
                    if (reader.Name == "row")
                    {

                        if (dt.Columns.Count == 0)
                        { // create the columns if we have none. 
                            while (reader.MoveToNextAttribute())
                            {
                                dt.Columns.Add(reader.Name);
                                Console.WriteLine("Atrributes and Values: "+ reader.Name + "=" + reader.Value);
                            }
                            reader.MoveToElement();
                        }

                        //string str = reader.GetAttribute(4).ToString();
                        dr = dt.NewRow();
                        for (int i = 0; i < reader.AttributeCount; i++)
                            dr[i] = reader.GetAttribute(i).ToString();
                        dt.Rows.Add(dr);
                    }
                }

            }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); }

            reader.Close();

        }