Tuesday, February 28, 2017

How to create an XML file in C#

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();
        }
    }
}

How to use C# Directory Class

using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Directory.Exists("c:\\testDir1"))
            {
                //shows message if testdir1 exist 
                MessageBox.Show ("Directory 'testDir' Exist ");
            }
            else
            {
                //create the directory testDir1 
                Directory.CreateDirectory("c:\\testDir1");
                MessageBox.Show("testDir1 created ! ");
                //create the directory testDir2 
                Directory.CreateDirectory("c:\\testDir1\\testDir2");
                MessageBox.Show("testDir2 created ! ");
                //move the directory testDir2 as testDir in c:\ 
                Directory.Move("c:\\testDir1\\testDir2", "c:\\testDir");
                MessageBox.Show("testDir2 moved ");
                //delete the directory testDir1 
                Directory.Delete("c:\\testDir1");
                MessageBox.Show("testDir1 deleted ");
            }
        }
    }
}

How to C# String Null

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
   string str =null;

   if (str == null)
   {
    MessageBox.Show("String is null");
   }
   else
   {
    MessageBox.Show("String is not null");
   }

   str = "test";

   if (string.IsNullOrEmpty(str))
   {
    MessageBox.Show("String is empty or null");
   }
   else
   {
    MessageBox.Show("String is not empty or null");
   }

  }
 }
}

How to use C# string Copy

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
   string str1 = null;
   string str2 = null;

   str1 = "CSharp Copy() test";
   str2 = string.Copy(str1);

   MessageBox.Show(str2); 
  }
 }
}


When you run this C# source code you will get same content of str1 in str2 

How to use C# string Equals

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
   string str1 = "Equals";
   string str2 = "Equals";

   if (string.Equals(str1, str2))
   {
    MessageBox.Show("Strings are Equal() ");
   }
   else
   {
    MessageBox.Show("Strings are not Equal() ");
   }
  }
 }
}


When you run this C# program you will get message "Strings are Equal() " 

How to use C# string Insert

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = "This is CSharp Test";
            string insStr = "Insert ";
            string strRes = str.Insert(15, insStr);
            MessageBox.Show(strRes); 
        }
    }
}


When you execute this C# program you will get the message box 
showing "This is CSharp Insert Test" 

How to use C# string Contains

using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = null;
            str = "CSharp TOP 10 BOOKS";
            if (str.Contains("TOP") == true)
            {
                MessageBox.Show("The string Contains() 'TOP' ");
            }
            else
            {
                MessageBox.Show("The String does not Contains() 'TOP'");
            } 
        }
    }
}


When you run the C# program you will get the MessageBox with message 
"The string Contains() 'TOP' "