I am currently upgrading my XML book that I wrote a decade ago to .NET 4.5 and you will be seeing many more articles on XML and .NET. A few days ago, I wrote How to Load XML File into a DataGridView Control and now here is the reverse method.
This article shows how to write DataSet contents to an XML file. The DataSet class contains methods to write a XML file from a DataSet object and fill the data to the file.
The WriteXml Method
The WriteXml method writes the current data (the schema and data) of a DataSet object to an XML file. This is an overloaded method. By using this method, you can write data to a file, stream, TextWriter, or XmlWriter.
The following code snippet writes a DataSet contents to a XML file using the WriteXml method:
- DataSet ds = CreateDynamicDataSet();
- ds.WriteXml(@"C:\Books\Students.xml");
Create a Windows Forms or WPF application and add a Button control to the Form/Window. Write the following code snippet for the button control click event handler.
The CreateDynamicDataSet method creates a DataSet object in-memory. If you already have data coming from a database, getting filled in a DataSet, you won't need this method. All you need to do is call the WriteXml method of your DataSet and pass the full path of the XML file.
This example creates a DataSet, fills the data for the DataSet, and writes the data to an XML file.
- private void WriteXmlButton_Click(object sender, EventArgs e)
- {
- DataSet ds = CreateDynamicDataSet();
- ds.WriteXml(@"C:\Books\Students.xml");
- }
- private DataSet CreateDynamicDataSet()
- {
- DataSet ds = new DataSet("DS");
- ds.Namespace = "StdNamespace";
- DataTable stdTable = new DataTable("Student");
- DataColumn col1 = new DataColumn("Name");
- DataColumn col2 = new DataColumn("Address");
- stdTable.Columns.Add(col1);
- stdTable.Columns.Add(col2);
- ds.Tables.Add(stdTable);
- //Add student Data to the table
- DataRow newRow; newRow = stdTable.NewRow();
- newRow["Name"] = "Mahesh Chand";
- newRow["Address"] = "Meadowlake Dr, Dtown";
- stdTable.Rows.Add(newRow);
- newRow = stdTable.NewRow();
- newRow["Name"] = "Mike Gold";
- newRow["Address"] = "NewYork";
- stdTable.Rows.Add(newRow);
- newRow = stdTable.NewRow();
- newRow["Name"] = "Mike Gold";
- newRow["Address"] = "New York";
- stdTable.Rows.Add(newRow);
- ds.AcceptChanges();
- return ds;
- }

Vanadana PPosted Sep 27, 2017, 8:15 AM
Hi, i want to add attribute to xml tag please have alook in my xml format that i have to generate using dataset. <?xml version="1.0" encoding="UTF-8"?> <package xmlns="http://apple.com/itunes/importer" version="film5.2"> <provider>applemusic</provider> <language>en-US</language> <video> <type>film</type> <subtype>feature</subtype> <vendor_id>Process_Feature_01_Clean</vendor_id> <country>US</country> <title>Process</title> <studio_release_title>Process</studio_release_title> <synopsis>Directed by Khalil Joseph, this visual companion to Sampha''s 2017 album Process is a collage of non-linear vingettes filmed in Morden, Sierra Leone and South London. Interspersing street scenes, dance performances, documetary-style footage, and music performances by Sampha, the 37-minute film features songs from the release.</synopsis> <production_company>Pulse Films</production_company> <ratings> <rating system="ag-movies" reason="Parental Guidance" code="PG"/> <rating system="au-oflc" reason="General" code="G"/> <rating system="br-movies" reason="Ominous theme" code="10"/> <rating system="de-fsk" reason="Approved for age 6 and older" code="6+"/> <rating system="hk-movies" reason="Neither obscene nor indecent" code="ALL"/> <rating system="in-movies" reason="12 years of age" code="UA"/> <rating system="nz-oflc" reason="Anyone can be shown or sold this" code="G"/> <rating system="zw-movies" reason="Parental Guidance" code="PG"/> </ratings> <crew> <crew_member billing="top"> <display_name>Kahlil Joseph</display_name> <roles> <role>Director</role> </roles> </crew_member> <crew_member billing="top"> <display_name>Kahlil Joseph</display_name> <roles> <role>Screenwriter</role> </roles> </crew_member> <!-- Additional crew members here --> </crew> </video> <video> <type>film</type> <subtype>feature</subtype> <vendor_id>808_Feature_01_Clean</vendor_id> <country>US</country> <title>808</title> <studio_release_title>808</studio_release_title> <synopsis>We love stories about innovation, and few innovations have changed the sound of music quite so dramatically as the Roland TR-808 drum machine. Narrated by Zane Lowe, this globe-spanning documentary is a music buff’s dream: we get firsthand accounts from some of the biggest artists of the last 40 years about how the all-powerful 808 shaped their biggest hits. More than just the study of a technological breakthrough, it reveals the interconnectedness of musical trends, the way forward-thinking producers and DJs are always listening to and improving upon each other’s breakthroughs—and creating entire new movements in the process.</synopsis> <production_company>You Know Films, Atlantic Films</production_company> <ratings> <rating system="ag-movies" reason="13 years of age" code="PG-13"/> <rating system="br-movies" reason="Not recommended for viewers under the age of 12" code="12"/> <rating system="de-fsk" reason="Approved for age 6 and older" code="6+"/> <rating system="hk-movies" reason="Neither obscene nor indecent" code="ALL"/> <rating system="in-movies" reason="Restricted to adults 18 and over" code="A"/> <rating system="nz-oflc" reason="M – Suitable for Mature Audiences 16 Years and Over" code="M"/> <rating system="zw-movies" reason="13 years of age" code="PG-13"/> </ratings> <crew> <crew_member billing="top"> <display_name>Alexander Dunn</display_name> <roles> <role>Director</role> </roles> </crew_member> <!-- Additional crew members here --> </crew> </video> </package>
Bernard Anyan MensahPosted Oct 27, 2013, 2:26 PM
Hi am new to c# and developing in .Net 2.0. I am trying to POST XML to a URL and read XML response, but I keep having errors from the HttpWebResponse class. Can anyone help me out ?