Friday, 22 March 2013

convert list to xml asp.net VB.NET using LINQ

convert list to xml asp.net VB.NET using LINQ

This example explains how to convert list type to XML using LINQ .

Create a class called Student

  Class Student
        Public Name As String,Grade As String
        Public year As Integer
  End Class

Add List items of type Student

             Dim stuList As List<Student> =  New List<Student>()
            stuList.Add(New Student
            {
                 Name="Shyam", Grade="A",year=1990
            }
)
            stuList.Add(New Student
            {
                 Name = "Raghu", Grade = "B", year = 2010
            }
)
            stuList.Add(New Student
            {
                 Name = "Paul", Grade = "C", year = 2001
            }
)
            stuList.Add(New Student
            {
                 Name = "murthy", Grade = "D", year = 2010
            }
)
            stuList.Add(New Student
            {
                 Name = "madam", Grade = "E", year = 2000
            }
)


Create XML from List

Dim root As New XElement("root")

For i As Integer = 0 To stuList.Count - 1
    Dim ele As New XElement("Student", New XElement("Name", stuList(i).Name), New XElement("Grade", stuList(i).Grade), New XElement("Year", stuList(i).year))
    root.Add(ele)
Next
Console.WriteLine(root)
OUTPUT:
<root>
  <Student>
    <Name>Shyam</Name>
    <Grade>A</Grade>
    <Year>1990</Year>
  </Student>
  <Student>
    <Name>Raghu</Name>
    <Grade>B</Grade>
    <Year>2010</Year>
  </Student>
  <Student>
    <Name>Paul</Name>
    <Grade>C</Grade>
    <Year>2001</Year>
  </Student>
  <Student>
    <Name>murthy</Name>
    <Grade>D</Grade>
    <Year>2010</Year>
  </Student>
  <Student>
    <Name>madam</Name>
    <Grade>E</Grade>
    <Year>2000</Year>
  </Student>
</root>
Tags:convert list to xml asp.net VB.NET using LINQ,convert list to xml asp.neT,convert list to xml VB.NET,LINQ to XML XElement usage.

convert list to xml asp.net C# using LINQ

convert list to xml asp.net C# using LINQ


This example explains how to convert list type to XML using LINQ .

Create a class called Student


    class Student
    {
        public String Name, Grade;
        public int year;
    }

Add List items of type Student


            List<Student> stuList = new List<Student>();
            stuList.Add(new Student { Name="Shyam", Grade="A",year=1990 });
            stuList.Add(new Student { Name = "Raghu", Grade = "B", year = 2010 });
            stuList.Add(new Student { Name = "Paul", Grade = "C", year = 2001 });
            stuList.Add(new Student { Name = "murthy", Grade = "D", year = 2010 });
            stuList.Add(new Student { Name = "madam", Grade = "E", year = 2000 });

Create XML from List


            XElement root = new XElement("root");

            for (int i = 0; i < stuList.Count; i++)
            {
                XElement ele = new XElement("Student", new XElement("Name", stuList[i].Name),
                                                        new XElement("Grade", stuList[i].Grade),
                                                        new XElement("Year", stuList[i].year));
                root.Add(ele);
            }
            Console.WriteLine(root);

OUTPUT:
<root>
  <Student>
    <Name>Shyam</Name>
    <Grade>A</Grade>
    <Year>1990</Year>
  </Student>
  <Student>
    <Name>Raghu</Name>
    <Grade>B</Grade>
    <Year>2010</Year>
  </Student>
  <Student>
    <Name>Paul</Name>
    <Grade>C</Grade>
    <Year>2001</Year>
  </Student>
  <Student>
    <Name>murthy</Name>
    <Grade>D</Grade>
    <Year>2010</Year>
  </Student>
  <Student>
    <Name>madam</Name>
    <Grade>E</Grade>
    <Year>2000</Year>
  </Student>
</root>

Tags:convert list to xml asp.net C# using LINQ,convert list to xml asp.ne,convert list to xml C#,LINQ to XML XElement usage.

Tuesday, 5 March 2013

Get Maximum Date using VB.NET LINQ


 Get Maximum Date using VB.NET LINQ

This example gets maximum/highest date from list of dates.

DateTime.Parse,  is used to Parse Date Strings  converts it into DateTime Object.
DateTime Custom Format:
         yyyy   is for four digit year
         MMM for  Month in Short form
         dd  is for Day . %d is for single digit/double digit day.


  Private  Sub GetMaxDate()
           String() szDates = New String() { "2013-jan-12",
               "2013-Mar-12", "2013-Feb-12",
               "2013-Apr-12", "2013-Oct-12","2013-Oct-12","2013-Nov-1",
               "2012-DEC-31"
               }



           var MaxDate = from dt in szDates
                          let custDt = DateTime.ParseExact(dt, "yyyy-MMM-%d",
                       System.Threading.Thread.CurrentThread.CurrentCulture)
                       orderby  custDt.Date descending
                       Dim dt As select

            Dim u As var
            For Each u In MaxDate
           Console.WriteLine("{0}",u)
            Next

            Console.WriteLine("Maxdate={0}",MaxDate.FirstOrDefault())
End Sub


OUTPUT:

2013-Nov-1
2013-Oct-12
2013-Oct-12
2013-Apr-12
2013-Mar-12
2013-Feb-12
2013-jan-12
2012-DEC-31
Maxdate=2013-Nov-1
LINQ let clause allows us to store temporary results in query sequence evaluation.
Tags:Get Maximum Date using VB.NET LINQ, LINQ Orderby on DateTime,LINQ LET CLAUSE,LINQ ORDERBY descending.How to get maximum date from selected dates.

Get Maximum Date using C# LINQ

 Get Maximum Date using C# LINQ

This example gets maximum/highest date from list of dates.

DateTime.Parse,  is used to Parse Date Strings  converts it into DateTime Object.
DateTime Custom Format:
         yyyy   is for four digit year
         MMM for  Month in Short form
         dd  is for Day .


  void GetMaxDate()
       {
           String[] szDates = new String[] { "2013-jan-12",
               "2013-Mar-12", "2013-Feb-12",
               "2013-Apr-12", "2013-Oct-12","2013-Oct-12","2013-Nov-1",
               "2012-DEC-31"};
         
          
           var MaxDate = from dt in szDates
                          let custDt = DateTime.ParseExact(dt, "yyyy-MMM-%d",
                       System.Threading.Thread.CurrentThread.CurrentCulture)
                       orderby  custDt.Date descending
                       select dt;

            foreach(var u in MaxDate)
           Console.WriteLine("{0}",u);

            Console.WriteLine("Maxdate={0}",MaxDate.FirstOrDefault());
       }

OUTPUT:

2013-Nov-1
2013-Oct-12
2013-Oct-12
2013-Apr-12
2013-Mar-12
2013-Feb-12
2013-jan-12
2012-DEC-31
Maxdate=2013-Nov-1
LINQ let clause allows us to store temporary results in query sequence evaluation.
Tags:Get Maximum Date using C# LINQ, LINQ Orderby on DateTime,LINQ LET CLAUSE,LINQ ORDERBY descending.How to get maximum date from selected dates.

Sunday, 3 March 2013

Reverse Words in a Sentence using VB.NET


Reverse Words in a Sentence using VB.NET

               This example explains how to get words by using delimiter ' ' and then reverse each word using String>reverse.
String.Split(new char{' '});//  Splits sentence  into an array words
String.Reverse  Reverses each word ,but returns IEnumerator<char> need to convert it into an Array of char. then re-create String with Array of characters.
 
Shared  Sub ReverseWordsInString()
          Dim original As String = "But,in a larger sense,we can not dedicate - 1
not consecrate = not consecrate - 1
          String()words=original.Split(New Char()
          {
              " "c
          }
)
          Dim i As Integer
          For  i = 0 To  words.Length- 1  Step i + 1
              Char()cWords = words(i).Reverse().ToArray()
              words(i) = New String(cWords)
          Next

          Dim word As String
          For Each word In words
              Console.WriteLine(word)
          Next

        original=String.Join(" ", words)
          Console.WriteLine(original)

 End Sub
 
String.Join static function joins by array of words into a String. 
Note: We used same delimiter for splitting and joining words.

OUTPUT

Input:But, in a larger sense, we can not dedicate-- we can
not consecrate-- we can not hallow-- this ground.
Output:,tuB ni a regral ,esnes ew nac ton --etacided ew ton
nac --etarcesnoc ew nac ton --wollah siht .dnuorg
Tags:  Reverse Words in a Sentence using VB.NET, Reverse word in VB.NET ,String.Reverse ,String.Join,Create new String using char array VB.NET.

Reverse Words in a Sentence using C#

Reverse Words in a Sentence using C#


               This example explains how to get words by using delimiter ' ' and then reverse each word using String>reverse.

String.Split(new char{' '});//  Splits sentence  into an array words
String.Reverse  Reverses each word ,but returns IEnumerator<char> need to convert it into an Array of char. then re-create String with Array of characters.
 
 static void ReverseWordsInString()
      {
          string original =@"But, in a larger sense, we can not dedicate-- we can
not consecrate-- we can not hallow-- this ground.";
          String[]words=original.Split(new char[] {' '});
          for(int i=0; i < words.Length;i++)
          {
              char[]cWords = words[i].Reverse().ToArray();
              words[i] = new String(cWords);
          }

          foreach (String word in words)
          {
              Console.WriteLine(word);
          }
        original=String.Join(" ", words);
          Console.WriteLine(original);
}


String.Join static function joins by array of words into String. 
Note: We splitted by delimter space ' ', same delimiter used for joining words.

OUTPUT

Input:But, in a larger sense, we can not dedicate-- we can
not consecrate-- we can not hallow-- this ground.

Output:,tuB ni a regral ,esnes ew nac ton --etacided ew ton
nac --etarcesnoc ew nac ton --wollah siht .dnuorg

Tags:  Reverse Words in a Sentence using C#, Reverse word in C# ,String.Reverse ,String.Join,Create new String using char array C#.

Generate Random Date using VB.NET

Generate Random Date using  VB.NET

 
 
This example generates random date from 1900 to 9999.   
DaysInMonth will be returned appropriate Days, based on Leap year also.




Private Function GenererateRandomDate() As DateTime

            Dim year As Integer =  rnd.Next(1900,9999)
            Dim month As Integer =  rnd.Next(1,12)
            Dim day As Integer =  DateTime.DaysInMonth(year,month)

            Dim Day As Integer =  rnd.Next(1,day)

            Dim dt As DateTime =  New DateTime(year,month,Day)
            Return dt
 End Function


Construct Random Class object Outside of the function,Make n number of calls.




OUTPUT:
Random Date=10/11/5427 12:00:00 AMRandom Date=6/22/4602 12:00:00 AMRandom Date=9/7/7710 12:00:00 AMRandom Date=10/12/7657 12:00:00 AMRandom Date=8/23/2410 12:00:00 AM
 

Note: User can apply  Standard Date Formats on this.
for ex:  rndDate.ToString("d")   displays short date:  2/18/5335 
other available formats are: o,O,g,G,d,D,s,u,U,f,F,Y,y .


Tags:Generate Random Dates in Vb.NET,  Generate Random Dates in various format,Random Date in Vb.NET.Generate Random Month,Generate random Day,Generate Random Year,How to Generate Random DateTime Object,Generate Random UTC Dates

Generate Random Date using C#

Generate Random Date using C#


This example generates random date from 1900 to 9999.   
if Month is February checks for Leap year so days will 29 otherwise 28.

        DateTime GenererateRandomDate()
        {
          
            int year = rnd.Next(1900, 9999);
            int month = rnd.Next(1, 12);
            int day = DateTime.DaysInMonth(year,month);
          
            int Day = rnd.Next(1, day);

            DateTime dt = new DateTime(year,month,Day);
            return dt;
        }

Construct Random Class object Outside of the function,Make n number of calls.




OUTPUT:

Random Date=10/11/5427 12:00:00 AMRandom Date=6/22/4602 12:00:00 AMRandom Date=9/7/7710 12:00:00 AMRandom Date=10/12/7657 12:00:00 AMRandom Date=8/23/2410 12:00:00 AM
 

Note: User can apply  Standard Date Formats on this.

for ex:  rndDate.ToString("d")   displays short date:  2/18/5335 

other available formats are: o,O,g,G,d,D,s,u,U,f,F,Y,y .


Tags: Generate Random Dates in C#,  Generate Random Dates in various format,Random Date in C#.Generate Random Month,Generate random Day,Generate Random Year,How to Generate Random DateTime Object,Generate Random UTC Dates

Generate Random String using VB.NET


Generate Random String using VB.NET


This tutorial generates Random Strings may be suitable for Password generation/Captcha etc.,

// This example uses Random class generates sequence of bytes and then converts those bytes to
//String, picks only Alphabets.
//
        Private Function GenerateRandomString() As String           
           Dim bytes() As Byte =  New Byte(255) {}
            rnd.NextBytes(bytes)
            Dim szRandom As String =  System.Text.Encoding.ASCII.GetString(bytes)
            Dim c() As Char =  szRandom.ToCharArray()
            Dim sb As StringBuilder =  New StringBuilder()
            Dim cc As Char
            For Each cc In c
                If Char.IsLetter(cc) Then
                    sb.Append(cc)
                End If
            Next
            Return sb.ToString()
        End Function
 
Dim rnd As Random =  New Random
Console.WriteLine("RandomPassword={0}",GenerateRandomString());

Output:
RandomPassword=WTdavEYQqzBekgKkqgJalgQGsgSJVdiQgnJzwwrlbJe
RandomPassword=dJpjgBczezrQSMLzkYhLoZRJdStitRFoHdmJikqhfGcxlrniKChEkI
RandomPassword=DGaYmSvZuHoHRMjVfvQrrVzxoDiQdvwkjDgKjAQXRGUEx
RandomPassword=mZABWuDFUHHEzVVmSchQHFnbOfXPpBMcxRfrFKqtzvJNTnurjHYz
RandomPassword=IzYTMyvMpmNaDqXRNFVtIgnHfVVEioYbUTkyIRSASDvbgcGoYlJBpuqp
RandomPassword=SLkyLRSLhDOkXztdIGIOVRkjqwTdfkqYUMQszCtnTNWiwcWVPngmBxRge


For Random AlphaNumeric  Strings


Change the Logic to
   Char.IsLetterOrDigit(cc)

Output:a6Nna0eeJaXW2K8LNAfBMTBmgSd2YoezGODa1v37sWlsAlL19CpQwkGVdDmdwIdzKbX

For Random Numbers.

  U can use same Random class,Each time it generates new number, otherwise,
Chanage the Logic like this
Char.IsNumber(cc)
Output:
2752175506
6767
578799
89989878
Tags:    Generate Random String using VB.NET,Generate Random Passwords,Generate Random Numbers VB.NET, Random String Vb.NET,Generate Random Alphanumeric String VB.NET,How to Generate Random String in VB.NET

Generate Random String using C#

Generate Random String using C#


This tutorial generates Random Strings may be suitable for Password generation/Captcha etc.,

// This example uses Random class generates sequence of bytes and then converts those bytes to
//String, picks only Alphabets.
//
        String GenerateRandomString()
        {            
           byte[] bytes = new byte[255];
            rnd.NextBytes(bytes);
            String szRandom = System.Text.Encoding.ASCII.GetString(bytes);
            char[] c = szRandom.ToCharArray();
            StringBuilder sb = new StringBuilder();
            foreach (char cc in c)
            {
                if (Char.IsLetter(cc))
                {
                    sb.Append(cc);
                }
            }            return sb.ToString(); ;
        }
Random rnd = new Random();
Console.WriteLine("RandomPassword={0}",GenerateRandomString());

Output:
RandomPassword=WTdavEYQqzBekgKkqgJalgQGsgSJVdiQgnJzwwrlbJe
RandomPassword=dJpjgBczezrQSMLzkYhLoZRJdStitRFoHdmJikqhfGcxlrniKChEkI
RandomPassword=DGaYmSvZuHoHRMjVfvQrrVzxoDiQdvwkjDgKjAQXRGUEx
RandomPassword=mZABWuDFUHHEzVVmSchQHFnbOfXPpBMcxRfrFKqtzvJNTnurjHYz
RandomPassword=IzYTMyvMpmNaDqXRNFVtIgnHfVVEioYbUTkyIRSASDvbgcGoYlJBpuqp
RandomPassword=SLkyLRSLhDOkXztdIGIOVRkjqwTdfkqYUMQszCtnTNWiwcWVPngmBxRge


For Random AlphaNumeric  Strings


Change the Logic to

   Char.IsLetterOrDigit(cc)

Output:a6Nna0eeJaXW2K8LNAfBMTBmgSd2YoezGODa1v37sWlsAlL19CpQwkGVdDmdwIdzKbX


For Random Numbers.


  U can use same Random class,Each time it generates new number, otherwise,

Chanage the Logic like this

Char.IsNumber(cc)

Output:
2752175506
6767
578799
89989878

Tags:    Generate Random String using C#,Generate Random Passwords,Generate Random Numbers C#, Random String C#,Generate Random Alphanumeric String C#,How to Generate Random String in C#