//将xml转为Datable 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); //获取StrStream中的数据 Xmlrdr = new XmlTextReader(StrStream); //ds获取Xmlrdr中的数据 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 string DataTable2Xml(DataTable vTable) { if (null == vTable) return string.Empty; StringWriter writer = new StringWriter(); vTable.WriteXml(writer); string xmlstr = writer.ToString(); writer.Close(); return xmlstr; }原文链接:https://www.f2er.com/xml/296123.html