?
Solved

WriteXML from DataSet and modify nodes

Posted on 2011-03-02
6
Medium Priority
?
469 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
[X]
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
  • 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

741 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