Posted By

zvasanth on 12/29/07


Tagged

table data list node xml to from convert datatable XmlNodeList


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


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.