We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

zvasanth on 12/29/07


Tagged

table data list node xml to from convert datatable XmlNodeList


Versions (?)


Convert XmlNodeList To DataTable


Published in: C# 


URL: ideabubbling.com

Everything is either true or untrue, or both true and untrue, or neither true nor untrue


  1. public static DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
  2. {
  3. DataTable dt = new DataTable();
  4. int TempColumn = 0;
  5.  
  6. foreach (XmlNode node in xnl.Item(0).ChildNodes)
  7. {
  8. TempColumn++;
  9. DataColumn dc = new DataColumn(node.Name, System.Type.GetType("System.String"));
  10. if (dt.Columns.Contains(node.Name))
  11. {
  12. dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
  13. }
  14. else
  15. {
  16. dt.Columns.Add(dc);
  17. }
  18. }
  19.  
  20. int ColumnsCount = dt.Columns.Count;
  21. for (int i = 0; i < xnl.Count; i++)
  22. {
  23. DataRow dr = dt.NewRow();
  24. for (int j = 0; j < ColumnsCount; j++)
  25. {
  26. dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
  27. }
  28. dt.Rows.Add(dr);
  29. }
  30. return dt;
  31. }

Report this snippet 

You need to login to post a comment.