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</LastNam e>
<FirstName>Nancy</FirstNam e>
...
</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
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</LastNam
<FirstName>Nancy</FirstNam
...
</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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm sorry but it anymore begins with <NewDataSet> ...
replace line 16 to:
dt.WriteXml(streamWrite);
dt.WriteXml(streamWrite);
ASKER
Still the same with dt.WriteXml:
<NewDataSet>
<Employees>
<EmployeeID>1</EmployeeID>
<LastName>Davolio</LastNam e>
<FirstName>Nancy</FirstNam e>
<Title>Sales Representative</Title>
</Employees>
<NewDataSet>
<Employees>
<EmployeeID>1</EmployeeID>
<LastName>Davolio</LastNam
<FirstName>Nancy</FirstNam
<Title>Sales Representative</Title>
</Employees>
ASKER
Just one more thing to do:
DataSet ds = new DataSet("Employees");
Thanks
DataSet ds = new DataSet("Employees");
Thanks
DataSet ds = new DataSet("Employees");