DataTable xml 互相转换

 

 

//测试方法

public static DataTable test()
{
string savePath = System.AppDomain.CurrentDomain.BaseDirectory;
string path="";
path=savePath+"/manager/wenjian.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path);
string str = doc.InnerXml;
DataTable dt = XmlToDataTable(str);
return dt;
}
//xml 转DataTable
public static DataTable XmlToDataTable(string xmlStr)
{
if (!string.IsNullOrEmpty(xmlStr))
{
StringReader StrStream = null;
XmlTextReader Xmlrdr = null;
try
{
DataSet ds = new DataSet();

StrStream = new StringReader(xmlStr);

Xmlrdr = new XmlTextReader(StrStream);

ds.readxml(Xmlrdr);
return ds.Tables[0];
}
catch (Exception e)
{
return null;
}
finally
{

if (Xmlrdr != null)
{
Xmlrdr.Close();
StrStream.Close();
StrStream.dispose();
}
}
}
return null;
}

//DataTable 转 xml public static void DataTabletoXml(DataTable vTable) { string savePath = System.AppDomain.CurrentDomain.BaseDirectory; if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string xml = savePath + @"\my.xml"; if (File.Exists(xml)) { File.Delete(xml); } vTable.WriteXml(savePath + @"\my.xml"); }

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念