Thursday, 2 May 2013

How To count number of elements stored in XML by TagName ..



//Create an XDocument Object 
XmlDocument xmlD = new XmlDocument();
xmlD.Load("give you XML Path Here");

//Ceate an object of node list which get nodes by tag name
XmlNodeList xmlNL = xmlD.GetElementsByTagName("tagname");

// You will get count of nodes available in selected XML 
int Count = xmlNL.Count;

Basic things with DataTable in C#

How Create DataTable : - 


        //Create DataTable
        DataTable dt = new DataTable();

        //Add Columns to DataTable 
        dt.Columns.Add("ID", typeof(Int32));
        dt.Columns.Add("Name"typeof(string));
        dt.Columns.Add("DOB"typeof(DateTime));

        // Add a row to datatable 
        dt.Rows.Add(1, "Developer", DateTime.Now);

        // You can Add row like below method too
        DataRow dr = dt.NewRow();

        dr["ID"] = 2;
        dr["Name"] = "Sagar";
        dr["DOB"] = DateTime.Now;

        //this will add the row at the end of the datatable
        dt.Rows.Add(dr);
        
        //By this wayyou can get sorted DataTable According to column either in ascending order or decending order
        dt.DefaultView.Sort = "ID asc";

Now if you want to an Column in Existing DataTable with Default value you can easily do that by using single line syntax :-

//Add Column To your existing DataTable
        DataColumn dc = new DataColumn(“IsActive”, typeof(bool));
       dc.DefaultValue = true;  // You can set default value from here
       dt.Columns.Add(dc);


If you want to search an specific thing to DataTable : - 

//This will return specific row if exist into new DataTable
DataTable dtNew= dt.Select("ID="+TextBox1.Text).CopyToDataTable();