C# 實現 DataTable 轉換成 XML字符串 public string ConvertDataTableToXml(DataTable dt) { StringBuilder strXml = new StringBuilder(); strXml.AppendLine("<MonitorData>"); for (int i = 0; i < dt.Rows.Count; i++) { strXml.AppendLine("<rows>"); for (int j = 0; j < dt.Columns.Count; j++) { strXml.AppendLine("<" + dt.Columns[j].ColumnName + ">" + dt.Rows[i][j] + "</" + dt.Columns[j].ColumnName + ">"); } strXml.AppendLine("</rows>"); } strXml.AppendLine("</MonitorData>"); return strXml.ToString(); } C# 實現 XML字符串 轉換成 DataTable private DataTable ConvertXmlToDataTable(string xmlData) { StringReader stream = null; XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); stream = new StringReader(xmlData); reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); return xmlDS.Tables[0]; } catch (Exception ex) { string strTest = ex.Message; return null; } finally { if (reader != null) reader.Close(); } }