DataTableToXml

private static string DataTableToXml(DataTable table)
        {
            if (null == table) return string.Empty;
            string xmlStr = "<ROOT>\n";
            for (int i = 0; i < table.Columns.Count; i++)
            {
                xmlStr += "<DATA_HEADER><DATA_NAME>" + table.Columns[i].ColumnName + "</DATA_NAME><DATA_TYPE>" + table.Columns[i].DataType + "</DATA_TYPE></DATA_HEADER>\n";
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                xmlStr += "<DATA_BODY>\n    ";
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    xmlStr += "<" + table.Columns[j].ColumnName + ">" + table.Rows[i][j] + "</" + table.Columns[j].ColumnName + ">";
                }
                xmlStr += "\n</DATA_BODY>\n";
            }
            xmlStr += "</ROOT>";
            return xmlStr;
        }
View Code

 

posted @ 2023-02-07 17:32  宋佳莉  阅读(6)  评论(0编辑  收藏  举报