日期:2014-05-16 浏览次数:20872 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Test2
{
class Program
{
static void Main(string[] args)
{
//1.将DataTable写入Xml
List<Person> list = new List<Person> {
new Person(){Id=1,Name="小明"},
new Person(){Id=2,Name="小华"}
};
DataTable dt = new DataTable("MatrixInfo");
DataColumn dc1 = new DataColumn("ID");
DataColumn dc2 = new DataColumn("Name");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
foreach(Person p in list){
DataRow dr = dt.NewRow();
dr["ID"] = p.Id;
dr["Name"] = p.Name;
dt.Rows.Add(dr);
}
System.IO.StringWriter writer = new System.IO.StringWriter();
dt.WriteXml(writer);
string xmlstr= writer.ToString();
writer.Close();
Console.WriteLine(xmlstr);
&n