Solved

WriteXML from DataSet and modify nodes

Posted on 2011-03-02
6
463 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:andre72
  • 3
  • 3
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 35017177
change line 10 to:
DataSet ds = new DataSet("Employees");
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 35017211
here's the full code:
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);
DataTable dt = ds.Tables[0];
dt.TableName="Employees";
                ds.WriteXml(streamWrite);
                streamWrite.Close();
                ds.Dispose();
                conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

Open in new window

0
 

Author Comment

by:andre72
ID: 35017283
I'm sorry but it anymore begins with <NewDataSet> ...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 42

Expert Comment

by:sedgwick
ID: 35017299
replace line 16 to:
dt.WriteXml(streamWrite);
0
 

Author Comment

by:andre72
ID: 35017367
Still the same with dt.WriteXml:
<NewDataSet>
  <Employees>
    <EmployeeID>1</EmployeeID>
    <LastName>Davolio</LastName>
    <FirstName>Nancy</FirstName>
    <Title>Sales Representative</Title>
  </Employees>
0
 

Author Comment

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

Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question