Process XML to DataTable Function

This code example demonstrates Csharp programming techniques and best practices.

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();

        }

Language: Csharp
Original Source: BlogEngine.NET Migration
Code Lines: 85

 

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

 

 

Process XML to DataTable Function

  This code can be used to use loop threw xml , grab a specifc set of nodes and dump them to &n