Solved

WriteXML from DataSet and modify nodes

Posted on 2011-03-02
6
461 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now