Wednesday 29 February 2012

How to bind Current Date Time to TextBlock - XAML

Create a Following Namespace 


xmlns:sys="clr-namespace:System;assembly=mscorlib"


 To Show the Current date time .


<TextBlock  Name="tbArrivalDateTime"
            Text="{Binding Source={x:Static sys:DateTime.Now},
                   StringFormat='{}{0:dd-MMM-yyyy hh:mm:ss}'}"
                   VerticalAlignment="Center"
                   Grid.Row="3"
                   Grid.Column="1"
                   Margin="10,0,0,0"
                   HorizontalAlignment="Left"/>

 To Show the Current date only .

<TextBlock  Name="tbArrivalDateTime"
            Text="{Binding Source={x:Static sys:DateTime.Today},
                   StringFormat='{}{0:dd-MMM-yyyy}'}"
                   VerticalAlignment="Center"
                   Grid.Row="3"
                   Grid.Column="1"
                   Margin="10,0,0,0"
                   HorizontalAlignment="Left"/>

Tuesday 28 February 2012

XML File reading Using XDocument in C#


Adding XML to Solution

1.       Right Click on Project
2.       Add
3.       New Item
4.       Select  XMLFile1.xml
5.       Right Click on created XML File
6.       Properties
7.        Set Build Action as “Embedded Resource”  (how the files relates to the build and deployment processes)

Sample XML 

<?xml version="1.0" encoding="utf-8" ?>

 <Books>

  <Book ID="1">
    <Name>Poohambam</Name>
    <Author>Mr Aasim Alavi</Author
    <Price>150</Price>    
  </Book>

<Book ID="2">
    <Name>C#</Name>
    <Author>Nick Wienholt</Author
    <Price>2300</Price>    
  </Book>
   
</Books>

Reading 

Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("TestProject.Books.xml");


            if (stream == null)
                return;
            XDocument data = XDocument.Load(stream);

foreach (XElement book in data.Root.Nodes())
{
        // TO DO :
        //int bookID = Convert.ToInt32(book.Attribute("ID").Value);
        //string bookName = book.Element("Name").Value;
        //decimal price = Convert.ToDecimal(book.Element("Price").Value);                 
}




Monday 20 February 2012

Where clause to a List Collection

      
Class
------
public class Student
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }


    public static List<Student> GetStudents()
    {
        List<Student> students = new List<Student>();
        students.Add(new Student { ID = 1,
                                   FirstName = "A",
                                   LastName = "F" });

        students.Add(new Student { ID = 2,
                                   FirstName = "B",
                                   LastName = "G" });

        students.Add(new Student { ID = 3,
                                   FirstName = "C",
                                   LastName = "H" });
        return students;
    }
}
      
      
Method Call
-----------
      
List<Student> studentList =  Student.GetStudents();
        
  
Filter
----------
int idForFilter = 1
List<Student> studentFiltered = studentList.Where(c => c.ID == idForFilter ).ToList();