Link to home
Start Free TrialLog in
Avatar of andre72
andre72

asked on

WriteXML from DataSet and modify nodes

Hi,

I like to write a DataSet to a file.
Is not such difficult as all the included source works fine.
The result is this:
<NewDataSet>
  <Table>
    <EmployeeID>1</EmployeeID>
    <LastName>Davolio</LastName>
    <FirstName>Nancy</FirstName>
...
  </Table>
</NewDataSet>

But what I need is this:
<?xml version="1.0" encoding="UTF-8" ?>
<Employees>
  <Employee EmployeeID="1" LastName="Davolio" FirstName="Nancy" .../>
</Employees>

Can I do this with WrtieXML and simple changes or how to get this result?

Thanks

Andre
string query = "SELECT * FROM Employees;";
            try
            {
                SqlConnection conn = new SqlConnection(String.Format("Data Source=(local);Initial Catalog={0};User={1};Password={2};", db, user, pw));
                SqlCommand cmd = new SqlCommand(query, conn);
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                string xmlFilename = @"C:\XmlDocument.xml";
                System.IO.FileStream streamWrite = new System.IO.FileStream(xmlFilename, System.IO.FileMode.Create);
                ds.WriteXml(streamWrite);
                streamWrite.Close();
                ds.Dispose();
                conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

Open in new window

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

change line 10 to:
DataSet ds = new DataSet("Employees");
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andre72
andre72

ASKER

I'm sorry but it anymore begins with <NewDataSet> ...
replace line 16 to:
dt.WriteXml(streamWrite);
Avatar of andre72

ASKER

Still the same with dt.WriteXml:
<NewDataSet>
  <Employees>
    <EmployeeID>1</EmployeeID>
    <LastName>Davolio</LastName>
    <FirstName>Nancy</FirstName>
    <Title>Sales Representative</Title>
  </Employees>
Avatar of andre72

ASKER

Just one more thing to do:
DataSet ds = new DataSet("Employees");

Thanks