SoFunction
Updated on 2025-03-07

C# Method to read data from the database to a DataSet and save it to an xml file

This article describes the method of C# reading data from a database to a DataSet and saving it to an xml file. Share it for your reference. The specific implementation method is as follows:

DataSet has a WriteXml method that can directly save data to an xml file

using System;
using ;
using ;
using ;
using ;
public class TestWriteXML
{
  public static void Main()
  {
    String strFileName = c:/temp/;
    SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
    String strSql = SELECT name,age FROM people;
    SqlDataAdapter adapter = new SqlDataAdapter();
     = new SqlCommand(strSql, conn);
    // Build the DataSet
    DataSet ds = new DataSet();
    (ds, employees);
    // Get a FileStream object
    FileStream fs = new FileStream(strFileName, , );
    // Apply the WriteXml method to write an XML document
    (fs);
    ();
  }
}

I hope this article will be helpful to everyone's C# programming.