将xml文件读入datagridview修改以后保存成新的xml文件怎么做?-.NET技术/C#
------回答---------
------其他回答(10分)---------
DataSet ds; load:
ds = new DataSet(); ds.ReadXml(filepath);
this.dataGridView1.DataSource = ds.Tables[0]; ……………… Save:
ds.AcceptChanges(); ds.WriteXml(filepath); ------其他回答(10分)---------
// 将xml文件转换为DataSet
public static DataSet ConvertXMLFileToDataSet(string xmlFile) {
StringReader stream = null; XmlTextReader reader = null; try {
XmlDocument xmld = new XmlDocument(); xmld.Load(xmlFile);
DataSet xmlDS = new DataSet(); stream = new StringReader(xmld.InnerXml); reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); return xmlDS; }
catch (System.Exception ex) {
throw ex; } finally
{
if (reader != null) reader.Close(); } }
// 将DataSet转换为xml文件
public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
MemoryStream stream = null; XmlTextWriter writer = null; try {
stream = new MemoryStream();
writer = new XmlTextWriter(stream, Encoding.Unicode); xmlDS.WriteXml(writer);
int count = (int)stream.Length; byte[] arr = new byte[count]; stream.Seek(0, SeekOrigin.Begin); stream.Read(arr, 0, count);
UnicodeEncoding utf = new UnicodeEncoding(); StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine( \ version=\\ \ encoding=\\ \ sw.WriteLine(utf.GetString(arr, 0, arr.Length).Trim()); sw.Close(); }
catch (System.Exception ex) {
throw ex; } finally {
if (writer != null) writer.Close(); } }
分别调用以上2个函数就可以了