Convert XmlNodeList To DataTable


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

URL: ideabubbling.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.