首先把数据读到dataset中 然后dataset进行变化后 在保存到xml中去 如何实现

解决方案 »

  1.   

    以下示例创建一个 System.IO.FileStream 对象。然后用 WriteXml 方法将该对象写入 XML 文档。
    [C#] 
    private void WriteXmlToFile(DataSet thisDataSet) {
       if (thisDataSet == null) { return; }
       // Create a file name to write to.
       string filename = "myXmlDoc.xml";
       // Create the FileStream to write with.
       System.IO.FileStream myFileStream = new System.IO.FileStream
          (filename, System.IO.FileMode.Create);
       // Write to the file with the WriteXml method.
       thisDataSet.WriteXml(myFileStream);   
    }
      

  2.   

    DataSet.ReadXml 方法[C#] 
    private void DemonstrateReadWriteXMLDocumentWithXMLReader(){
       // Create a DataSet with one table and two columns.
       DataSet OriginalDataSet = new DataSet("myDataSet");
        OriginalDataSet.Namespace= "NetFrameWork";
       DataTable myTable = new DataTable("myTable");
       DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
       c1.AutoIncrement= true;
       DataColumn c2 = new DataColumn("item");
       myTable.Columns.Add(c1);
       myTable.Columns.Add(c2);
       OriginalDataSet.Tables.Add(myTable);
       // Add ten rows.
       DataRow newRow;
       for(int i = 0; i < 10; i++){
          newRow = myTable.NewRow();
          newRow["item"]= "item " + i;
          myTable.Rows.Add(newRow);
       }
       OriginalDataSet.AcceptChanges();
       // Print out values of each table in the DataSet using the 
       // function defined below.
       PrintValues(OriginalDataSet, "Original DataSet");
       // Write the XML schema and data to file with FileStream.
       string xmlFilename = "myXmlDocument.xml";
       // Create FileStream    
       System.IO.FileStream fsWriteXml = new System.IO.FileStream
          (xmlFilename, System.IO.FileMode.Create);
       // Create an XmlTextWriter to write the file.
       System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
          (fsWriteXml, System.Text.Encoding.Unicode);
       // Use WriteXml to write the document.
       OriginalDataSet.WriteXml(xmlWriter);
       // Close the FileStream.
       fsWriteXml.Close();
          
       // Dispose of the original DataSet.
       OriginalDataSet.Dispose();
       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");
          
       // Read the XML document back in. 
       // Create new FileStream to read schema with.
       System.IO.FileStream fsReadXml = new System.IO.FileStream
          (xmlFilename, System.IO.FileMode.Open);
       // Create an XmlTextReader to read the file.
       System.Xml.XmlTextReader myXmlReader = 
          new System.Xml.XmlTextReader(fsReadXml);
       // Read the XML document into the DataSet.
       newDataSet.ReadXml(myXmlReader);
       // Close the XmlTextReader
       myXmlReader.Close();   // Print out values of each table in the DataSet using the 
       // function defined below.
       PrintValues(newDataSet,"New DataSet");
    }private void PrintValues(DataSet ds, string label){
       Console.WriteLine("\n" + label);
       foreach(DataTable t in ds.Tables){
          Console.WriteLine("TableName: " + t.TableName);
          foreach(DataRow r in t.Rows){
             foreach(DataColumn c in t.Columns){
                Console.Write("\t " + r[c] );
             }
             Console.WriteLine();
          }
       }
    }
      

  3.   

    参考--Edit and Save XML with a DataGrid:
    http://www.ragingsmurf.com/code.aspx?key=KI54D3MG8Y
      

  4.   

    thisDataSet.WriteXml("c:\myFilename.xml");
    thisDataSet.ReadXml("c:\yourFilename.xml");
      

  5.   

    大家都说的对,只能和大家贴一样的了^_^
    thisDataSet.WriteXml("c:\myFilename.xml");
    thisDataSet.ReadXml("c:\yourFilename.xml");
      

  6.   

    读xml文件:thisDataSet.ReadXml("c:\yourFilename.xml");
    会自动生成一些DataTable
    写xml文件:thisDataSet.WriteXml("c:\myFilename.xml");
      

  7.   

    a.aspx<%@ Page language="c#" Codebehind="a.aspx.cs" Src="a.aspx.cs" AutoEventWireup="false" Inherits="GM.a" %>
    <html>
      <head>
        <title>Lion互动网络==》DataSet写入XML文档设置编码级别(Encoding)</title>
      </head>
      <body> 
        <form id="Form1" method="post" runat="server">
         </form> 
      </body>
    </html>
    a.aspx.csusing System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace GM
    {
     public class a : System.Web.UI.Page
     {
      override protected void OnInit(EventArgs e)
      {
       System.Data.DataTable DT = new System.Data.DataTable("temptable");
       DT.Columns.Add("ID",typeof(int));
       DT.Columns.Add("Name",typeof(string));
       DT.Columns.Add("AddTime",typeof(DateTime));
       DT.Columns[0].AutoIncrement = true;
       DT.Columns[0].AutoIncrementSeed = 1;
       DT.Columns[0].AutoIncrementStep = 1;
       for(int i=0;i<20;i++)
       {
        System.Data.DataRow dr= DT.NewRow();
        dr[1] = "欢迎光临Lion互动网络,第 "+ (i+1) +" 行";
        dr[2] = System.DateTime.Now.AddDays(i);
        DT.Rows.Add(dr);
       }   
       System.Data.DataSet ds = new System.Data.DataSet("Root");
       ds.Tables.Add(DT); 
       System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
       doc.LoadXml(ds.GetXml());
       ds.Clear();
       ds.Dispose();
       System.Xml.XmlDeclaration xmldecl;
       xmldecl = doc.CreateXmlDeclaration("1.0",null,null);
       xmldecl.Encoding="GB2312";
       xmldecl.Standalone="yes";
       System.Xml.XmlElement root = doc.DocumentElement;
       doc.InsertBefore(xmldecl, root);
       doc.Save(Server.MapPath(".")+"file://a.xml/");
      }
     }
    }
      

  8.   

    using System;
    using System.Data;
    using System.Data.SqlClient;public class App
    {
      public static void Main()
      {
        string dataSource = "Data Source=localhost;";
        string security = "user id=sa; password=;";
        string initialCatalog = "initial catalog=Diablo;";
        string cnnString = dataSource + security + initialCatalog;
        SqlConnection connection = new SqlConnection(cnnString);    string strSql = " SELECT * from Character ";
        SqlCommand command = new SqlCommand(strSql, connection);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = command;    DataSet dataSet = new DataSet();
        try
        {
          connection.Open();
          adapter.Fill(dataSet);
        }
        catch(Exception e)
        {
          Console.WriteLine(e.ToString());
          return;
        } 
        finally
        {
          connection.Close();
        }
        dataSet.WriteXml("example.xml");
      }
    }