Wednesday, September 3, 2014

Serialize list of Objects in XML

public static class SerializeUtil
    {
        public static string XmlSerialize<T>(List<T> sourceCollection)
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(List<T>));
            StringWriter textWriter = new StringWriter();
            xmlSer.Serialize(textWriter, sourceCollection);
            xmlSer = null;
            return textWriter.ToString();
        }
    }

Convert from String to Datetime

DateTime dt= DateTime.ParseExact(value, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)

Read a comma separated file and populate a dictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileParsing
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader("D:/ID.txt");

            //Dictionary<string, string> dict = new Dictionary<string, string>();
            Dictionary<string, object> dict = new Dictionary<string, object>();

            Player player = new Player();
            while (reader.Peek() >= 0)
            {
                string line = reader.ReadLine();
                string[] data = line.Split(';');

                if (line != String.Empty)
                {
                    for (int i = 0; i < data.Length - 1; i++)
                    {
                        string key = data[i].Trim().Split('=')[0];
                        string value = data[i].Trim().Split('=')[1];
                        string dictKey=string.Empty;
                        switch (key)
                        {
                            case "FNAME": dictKey="FirstName"; break;
                            case "LNAME": dictKey = "LastName"; break;
                            case "MNAME": dictKey="MiddleName"; break;
                            case "ADD": dictKey="Add"; break;
                            case "CITY": dictKey="City"; break;
                            case "ST": dictKey="State"; break;
                            case "ZIP": dictKey="Zip"; break;
                            case "DOB": dictKey = "DOB"; break;
                            case "DOE": dictKey="DOE"; break;
                            case "DOI": dictKey="DOI"; break;
                            case "DLNO": dictKey="DLNo"; break;
                            case "SEX": dictKey="Sex"; break;
                            default:
                                break;
                        }
                        if(!dict.ContainsKey(dictKey))
                            dict.Add(dictKey, value);
                     
                    }
                   
                    //dict.Add(playerCount, player);
                }
            }
        }
    }

    class Player
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string Add { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public DateTime DOB { get; set; }
        public string DOE { get; set; }
        public string DOI { get; set; }
        public string DLNo { get; set; }
        public string Sex { get; set; }
    }
}

Validate Number in MVC

function ValidateNumber(e) {
            var evt = (e) ? e : window.event;
            var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
            if (charCode > 31 && (charCode < 48 || charCode > 57)) {
                return false;
            }
            return true;
        };


@Html.TextBoxFor(model => model.Vendor.FaxNumber, new { @class = "form-control", @maxlength = "15", onkeydown = "return ValidateNumber(event);" })