Monday, 2 December 2013

How to create XML file using C#

Leave a Comment
XML is a platform independent language, so the information formatted in XML can be used in any other platforms (Operating Systems). Once we create an XML file in one platform it can be used in other platforms also.

In order to creating a new XML file in C#, we are using XmlTextWriter Class . The class takes FileName and Encoding as argument. Also we are here passing formatting details . The following C# source code creating an XML file product.xml and add four rows in the file

The Code

using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
namespace WindowsApplication1
{
   public partial class Form1 : Form
   {
      public Form1()
      {
          InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
          XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
          writer.WriteStartDocument(true);
          writer.Formatting = Formatting.Indented;
          writer.Indentation = 2;
          writer.WriteStartElement("Table"); 
          createNode("1", "Product 1", "1000", writer);
          createNode("2", "Product 2", "2000", writer);
          createNode("3", "Product 3", "3000", writer);
          createNode("4", "Product 4", "4000", writer);
          writer.WriteEndElement();
          writer.WriteEndDocument();
          writer.Close();
          MessageBox.Show("XML File created ! ");
      }

      private void createNode(string pID, string pName, string pPrice, XmlTextWriter writer)
     {

         writer.WriteStartElement("Product");
         writer.WriteStartElement("Product_id");
         writer.WriteString(pID);
         writer.WriteEndElement();
         writer.WriteStartElement("Product_name");
         writer.WriteString(pName);
         writer.WriteEndElement();
         writer.WriteStartElement("Product_price");
         writer.WriteString(pPrice);
         writer.WriteEndElement();
         writer.WriteEndElement();
      }
   }
}

Output Example

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
  <Product>
    <Product_id>1</Product_id>
    <Product_name>Product 1</Product_name>
    <Product_price>1000</Product_price>
  </Product>
  <Product>
    <Product_id>2</Product_id>
    <Product_name>Product 2</Product_name>
    <Product_price>2000</Product_price>
  </Product>
  <Product>
    <Product_id>3</Product_id>
    <Product_name>Product 3</Product_name>
    <Product_price>3000</Product_price>
  </Product>
  <Product>
    <Product_id>4</Product_id>
    <Product_name>Product 4</Product_name>
    <Product_price>4000</Product_price>
  </Product>
</Table>

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.