Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

WriteXML from DataSet and modify nodes

Posted on 2011-03-02
6
464 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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