Friday, November 20, 2009

String Format for DateTime [C#]



c# Examples


Menu:















String Format for DateTime [C#]



This example shows how to format DateTime
using String.Format
method. All formatting can be done also using DateTime.ToString
method.



Custom DateTime Formatting



There are following custom format specifiers y (year),
M (month), d (day), h (hour 12),
H (hour 24), m (minute), s (second),
f (second fraction), F (second fraction, trailing
zeroes are trimmed), t (P.M or A.M) and z
(time zone).



Following examples demonstrate how are the format specifiers rewritten to the
output.


[C#]



// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone



You can use also date separator / (slash) and
time sepatator : (colon). These characters will be
rewritten to characters defined in the current DateTimeForma­tInfo.DateSepa­rator
and DateTimeForma­tInfo.TimeSepa­rator.


[C#]



// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)



Here are some examples of custom date and time formatting:


[C#]



// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"

// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008"

// two/four digit year
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"



Standard DateTime Formatting



In DateTimeForma­tInfo
there are defined standard patterns for the current culture. For example
property ShortTimePattern
is string that contains value h:mm tt for en-US
culture and value HH:mm for de-DE culture.



Following table shows patterns defined in DateTimeForma­tInfo
and their values for en-US culture. First column contains format specifiers for
the String.Format
method.



























































































































Specifier DateTimeFormatInfo property Pattern value (for en-US culture)
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
T LongTimePattern h:mm:ss tt
D LongDatePattern dddd, MMMM dd, yyyy
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g (combination of d and t) M/d/yyyy h:mm tt
G (combination of d and T) M/d/yyyy h:mm:ss tt
m, M MonthDayPattern MMMM dd
y, Y YearMonthPattern MMMM, yyyy
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u UniversalSorta­bleDateTimePat­tern yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)
(*) = culture independent


Following examples show usage of standard format specifiers
in String.Format
method and the resulting output.


[C#]



String.Format("{0:t}", dt); // "4:05 PM" ShortTime
String.Format("{0:d}", dt); // "3/9/2008" ShortDate
String.Format("{0:T}", dt); // "4:05:07 PM" LongTime
String.Format("{0:D}", dt); // "Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); // "3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); // "3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); // "March 09" MonthDay
String.Format("{0:y}", dt); // "March, 2008" YearMonth
String.Format("{0:r}", dt); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); // "2008-03-09T16:05:07" SortableDateTime
String.Format("{0:u}", dt); // "2008-03-09 16:05:07Z" UniversalSortableDateTime



See also







By Jan Slama, 25-Mar-2008














WPF Timer (DispatcherTimer)


This article discusses the WPF DispatcherTimer.

Most of us are from a background of C# (or VB.Net). You must have some point of time come across a scenario for performing a repetitive GUI operation after certain period of time. The solution in most cases has been the WinForm's Timer.

What about WPF ? Does it have a Timer ?

WPF no different to WinForm has it's own Timer equivalent. It is called DispatcherTimer. There are other options to get the Timer behavior but one of the most efficient for a Window based operation is the DispatcherTimer.

I have attached a sample Timer application demonstrating the functionality.

The window has a "Start timer" and "Stop timer" functionality. There is an input TextBox which takes the Timer frequency in milliseconds.

Just enter the Timer frequency in millisecond and hit the "Start timer" button. There is a status bar which displays the status of the time. At start it will be blank. When you start the Timer it will display the current Time and when you stop the Timer it will display the "Stopped" status.

Let us discuss the code to look into how we use it.

XAML,
< Window x:Class="TimerSample.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Timer" Height="148" Width="300" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
   
       
           
           1000
           
       
       
           
           
       
       Not working
   



Code behind,


public partial class Window1 : Window
{
   DispatcherTimer _timer;

   public Window1()
   {
       InitializeComponent();

       _timer = new DispatcherTimer();
       _timer.Tick += new EventHandler(delegate(object s, EventArgs a)
       {
           status.Text = "Started: " + DateTime.Now;
           //MessageBox.Show("Dummy message"); //notify timer end
           //this.Close(); //close application
       });
   }

   private void Start_Click(object sender, RoutedEventArgs e)
   {
       // Set the Interval
       _timer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(time.Text));

       // Start the timer
       _timer.Start();

       status.Text = "Started";
   }

   private void Stop_Click(object sender, RoutedEventArgs e)
   {
       // Stop the timer
       _timer.Stop();

       status.Text = "Stopped";
   }
}


We have a local variable of type DispatcherTimer - _timer. We manipulate this variable to start and stop the timer. At start we initialize the variable with an Event delegate to fire whenever the Timer ticks.


_timer.Tick += new EventHandler(delegate(object s, EventArgs a)
       {
           status.Text = "Started: " + DateTime.Now;
           //MessageBox.Show("Dummy message"); //notify timer end
           //this.Close(); //close application
       });


I have specifically left the commented lines to show the different possibilities that can be done in the Event delegate. Whenever the Timer ticks the event is fired and Status bar is updated with the most recent DateTime.

DispatcerTimer runs on the UI thread and hence does not need marshaling back and forth as we have to do in the normal cases of a Thread.

Have fun.

For details click here.

Friday, July 10, 2009

C# Conditional Operator ( ?: ) and Null-Coalescing Operator ( ?? )

by David Hayden ( .NET C# Developer )

http://davidhayden.com/blog/dave/archive/2006/07/05/NullCoalescingOperator.aspx


Often times in code you want to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with another ( perhaps default ) value. The code normally may look like this using the C# Conditional Operator:

string fileName = tempFileName != null ?
tempFileName : "Untitled";

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

This can now be abbreviated as follows using the Null-Coalescing Operator:

string fileName = tempFileName ?? "Untitled";

The logic is the same. If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;  int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero ).

These Conditional and Null-Coalescing Operators aren't the most self-describing operators :), but I do love programming in C#!

Tuesday, June 16, 2009

convert wstring to wchar*

To convert std::wstring to wchar*  or LPWSTR the function is wstring.c_str() which returns wchar* This works fine in VC6 But in visual studio 2008(vc9) it returns garbage value. is there some other function to convert std::wstring to wchar*/LPWSTR.

Reference :- http://www.eggheadcafe.com/software/aspnet/33281579/convert-wstring-to-wchar.aspx

Wednesday, May 27, 2009

OSQL Utility

This article will explain and define the OSQL utilities ability to run scripts for administration and batch work when using Microsoft SQL Server.

Introduction

If you have repetitive tasks to run, multiple SQL servers to administer, or a very large sequence of commands to execute, then the OSQL utility may be the tool to use. OSQL is capable of running both scripts and interactive commands. It is started from the command line and can be executed manually or by a scheduled task. With over twenty-five switch commands, OSQL can usually be configured to execute, as your application requires. OSQL does not have a user interface. So many times, scripts are created in Query Analyzer, saved and then run by OSQL.

OSQL vs. ISQL and Query Analyzer

There is a great deal of overlap between ISQL and OSQL. Both support input scripts, output scripts, and most of the same switch arguments. OSQL has no interface. It will only accept a typed command line, or a saved script. In spite of this disadvantage, sometimes ISQL and Query Analyzer cannot accomplish the required task. Working with MSDE is one example. Query Analyzer is not included with the Microsoft Desktop Engine. When developing an application on MSDE, or needing to do MSDE administration, the OSQL utility is the only tool included. Another key difference between ISQL and OSQL is the base library each tool was built on. ISQL is developed on the DB Library, as opposed to OSQL being developed on ODBC. The DB Library works at the SQL 6.5 standard. This difference means ISQL, or any application developed on the DB Library, dose not support some of the new SQL 2000 features. The entire list of unsupported features can found in Books on Line under the title "Connecting Early Version Clients to SQL Server 2000." Some of the main limitations of ISQL include char and varchars defined greater than 255 bytes will be non accessible, big ints will be converted to decimals, sql_variants will be converted to nvarchars, XML results may not be retrieved, and bit fields that are null will be reported as not null with a value of 0. OSQL and Query Analyzer will support all of the SQL 2000 features.

OSQL ad hoc query examples

In these beginning tests, we will execute ad hoc queries from the command line. This first command assumes SQL server is local on your machine and you have Windows trusted connection, rather than a SQL uid and password. Open the command prompt and enter:
OSQL -E -Q "SELECT * FROM sysloings"
The contents of the syslogins table should scroll down the command window. In the above statement, -E tells OSQL to used a trusted connection rather than a SQL uid. The -Q is the query statement. Because no database was specified, master was used. To specify a database, change the statement to:
OSQL -E -d pubs -Q "SELECT * FROM authors"
The switches are case sensitive. Moreover, many times a lower case letter has no relation to an upper case letter. Lower case p is used for print performance statistics while an upper case P is used to specify a SQL password.
To use SQL security rather than Windows security, remove the -E and change the statement to:
OSQL -U sa -P secret  -d pubs -Q "SELECT * FROM authors"

OSQL scripts

In these next examples, we will create and save TSQL scripts, and then run them from OSQL. Query Analyzer is a standard choice for script creation because of the color coding. Open Query Analyzer and enter:
USE pubs GO SELECT * FROM authors GO 
Save this script to your hard drive, and then from the command line, enter an OSQL statement using the -i switch to specify an input file. The authors table should be returned.
OSQL -E -i c:\temp\q1.sql

The results of the query can be captured to an output file, rather than appearing on the screen. Change the command line to include the -o parameter, for output.
OSQL -E -i c:\temp\q1.sql -o c:\temp\resutls.txt
OSQL should create a text output file. The -u switch can be used to control the output file being either Unicode or OEM.

System Commands

Operating system commands can also be executed from inside the TSQL script. The key !! is used to specify this. Change the Query Analyzer script and save it as:
!! dir c:\temp GO USE pubs GO SELECT * FROM authors GO 
Now our output file will include the directory listing of the temp folder in addition to the authors' results. Also, note this script will not run in Query Analyzer. The !! directive is not supported. Query Analyzer color coding is helpful in code layout, but the script testing will need to be done from OSQL.

Error Handling

OSQL supports the RAISERROR command for returning custom error messages. To use raise error, the database name, id, error severity and state should be included. Using RAISERROR will cause the script to terminate. Modify the Query Analyzer script to:
!! dir c:\temp GO DECLARE @DBID int SET @DBID = DB_ID() DELCARE @DBNAME nvarchar(128) SET @DBNAME = DB_NAME() RAISERROR('my error', 18, 127, @DBID, @DBNAME) USE pubs GO SELECT * FROM authors GO 
Running the script will now output our directory listing, from the "!! dir c:\temp" command, followed by the raise error. The remaining script, changing to pubs and selecting from authors will not occur. RAISERROR will terminate the script.
Leaving a script can also be done by calling QUIT or EXIT from inside OSQL. Neither of these will return an error code, but EXIT can execute a statement prior to quitting. For example: EXIT(SELECT @@ROWCOUNT).

Conclusion

When administering the Microsoft Desktop Engine, OSQL is a free way to run statements. For standard SQL Server environments, OSQL can be used to help automate long or repetitive tasks by reusing scripts. OSQL is also a good choice to run database setup scripts during application install procedures.
Reference:-http://www.databasejournal.com/features/mssql/article.php/3403331/OSQL-Utility.htm by Don Schlichting 

Tuesday, May 26, 2009

LINQ TO TEXT AND LINQ TO CSV

Reference:-http://blogs.msdn.com/ericwhite/archive/2008/09/30/linq-to-text-and-linq-to-csv.aspx

LINQ is a great tool for writing ad-hoc queries and transforms, and occasionally I need to write queries or transforms on text files.  And sometimes I receive CSV files, and need to do something with them.  I wrote a blog post on LINQ to Text files over two years ago.  My coding practices today differ from what I presented in that blog post.  This post presents my current approach for dealing with text files using LINQ, and includes a function for splitting CSV lines.

In that post, I detailed an approach for writing lazy queries on text files.  Well, to tell you the truth, I never write lazy queries on text files – I simply use File.ReadAllLines and write queries over the returned string array.  After all, my computer has a lot of RAM (and probably yours does too), and the CSV files I receive are maybe in the 50K size, and are rarely greater than a few hundred K.  It simply doesn’t matter that I read the entire text file into memory.  If I were to write the exact same two queries today, here is how I would write them:

string[] lines = File.ReadAllLines("TextFile.txt");

var t1 = lines

    .Where(l => !l.StartsWith("#"))

    .Select(l => l.Split(','))

    .Select(items => String.Format("{0}{1}{2}",

        items[1].PadRight(16),

        items[2].PadRight(16),

        items[3].PadRight(16)));

var t2 = t1

    .Select(l => l.ToUpper());

foreach (var t in t2)

    Console.WriteLine(t);

 

I have a small extension method (CsvSplit) that I use to split lines that are in CSV format.  I’ve had this method around for a while – it’s not written in the functional style.  Instead, it’s a state machine.  I’ve thought about what it would take to rewrite this method in the functional style, and as far as I know, the only way to do it would be to define a grammar, and maybe write a recursive descent parser.  (Actually, there is another approach, but it would be very inefficient, and the code would be longer and less readable.)  Well the job of parsing CSV files simply isn’t worth the effort!  So a state machine it is.  In any case, the ‘functional impurity’ is local to the function.

The semantics of CsvSplit are:

  • CsvSplit is an extension method on the String class.
  • Only a comma is valid for the separator character.
  • Values can be quoted.  The quotes are trimmed.
  • Quoted values can have, of course, internal commas.  Quoted values can also have internal escape sequences: backslash followed by any character, including quote (\”), backslash (\\) or any other character (\a).
  • CsvSplit will throw an exception for incorrectly formatted strings.

If the CSV file that I receive isn’t in this format, then I just load it into Excel and save so that it is in this format.

If you have this for a source file:

Bob,"Bob said to go to the store."

Mary,"Mary said, \"Whatever.\""

Jim,Jim's quote doesn't contain quotes or commas.

 

Then you can query the CSV file like this:

var data = File.ReadAllLines("TextFile.txt")

    .Select(

        l => {

            var split = l.CsvSplit();

            return new {

                Person = split[0],

                Quote = split[1]

            };

        }

    );

 

foreach (var item in data)

    Console.WriteLine("{0}:{1}", item.Person, item.Quote);

 

The function is composable.  If you want to convert the CSV file to an XML file, you can do so like this:

XElement xmlDoc = new XElement("Root",

    File.ReadAllLines("TextFile.txt")

        .Select

        (

            line => {

                var split = line.CsvSplit();

                return new XElement("Quote",

                    new XElement("Person", split[0]),

                    new XElement("Text", split[1])

                );

            }

        )

);

Console.WriteLine(xmlDoc);

 

Here is the listing for CsvSplit (also attached):

public class CsvParseException : Exception

{

    public CsvParseException(string message)

        base(message)

    {

    }

}

 

public static class MyExtensions

{

    private enum State

    {

        AtBeginningOfToken,

        InNonQuotedToken,

        InQuotedToken,

        ExpectingComma,

        InEscapedCharacter

    };

 

    public static string[] CsvSplit(this String source)

    {

        List<string> splitString = new List<string>();

        List<int> slashesToRemove = null;

        State state = State.AtBeginningOfToken;

        char[] sourceCharArray = source.ToCharArray();

        int tokenStart = 0;

        int len = sourceCharArray.Length;

        for (int i = 0; i <>

        {

            switch (state)

            {

                case State.AtBeginningOfToken:

                    if (sourceCharArray[i] == '"')

                    {

                        state = State.InQuotedToken;

                        slashesToRemove = new List<int>();

                        continue;

                    }

                    if (sourceCharArray[i] == ',')

                    {

                        splitString.Add("");

                        tokenStart = i + 1;

                        continue;

                    }

                    state = State.InNonQuotedToken;

                    continue;

                case State.InNonQuotedToken:

                    if (sourceCharArray[i] == ',')

                    {

                        splitString.Add(

                            source.Substring(tokenStart, i - tokenStart));

                        state = State.AtBeginningOfToken;

                        tokenStart = i + 1;

                    }

                    continue;

                case State.InQuotedToken:

                    if (sourceCharArray[i] == '"')

                    {

                        state = State.ExpectingComma;

                        continue;

                    }

                    if (sourceCharArray[i] == '\\')

                    {

                        state = State.InEscapedCharacter;

                        slashesToRemove.Add(i - tokenStart);

                        continue;

                    }

                    continue;

                case State.ExpectingComma:

                    if (sourceCharArray[i] != ',')

                        throw new CsvParseException("Expecting comma");

                    string stringWithSlashes =

                        source.Substring(tokenStart, i - tokenStart);

                    foreach (int item in slashesToRemove.Reverse<int>())

                        stringWithSlashes =

                            stringWithSlashes.Remove(item, 1);

                    splitString.Add(

                        stringWithSlashes.Substring(1,

                            stringWithSlashes.Length - 2));

                    state = State.AtBeginningOfToken;

                    tokenStart = i + 1;

                    continue;

                case State.InEscapedCharacter:

                    state = State.InQuotedToken;

                    continue;

            }

        }

        switch (state)

        {

            case State.AtBeginningOfToken:

                splitString.Add("");

                return splitString.ToArray();

            case State.InNonQuotedToken:

                splitString.Add(

                    source.Substring(tokenStart,

                        source.Length - tokenStart));

                return splitString.ToArray();

            case State.InQuotedToken:

                throw new CsvParseException("Expecting ending quote");

            case State.ExpectingComma:

                string stringWithSlashes =

                    source.Substring(tokenStart, source.Length - tokenStart);

                foreach (int item in slashesToRemove.Reverse<int>())

                    stringWithSlashes = stringWithSlashes.Remove(item, 1);

                splitString.Add(

                    stringWithSlashes.Substring(1,

                        stringWithSlashes.Length - 2));

                return splitString.ToArray();

            case State.InEscapedCharacter:

                throw new CsvParseException("Expecting escaped character");

        }

        throw new CsvParseException("Unexpected error");

    }

}


The whole program is as follows:- LinqToCsv.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml.Linq;

 

namespace LinqToCsv

{

    public class CsvParseException : Exception

    {

        public CsvParseException(string message)

            : base(message)

        {

        }

    }

 

    public static class MyExtensions

    {

        private enum State

        {

            AtBeginningOfToken,

            InNonQuotedToken,

            InQuotedToken,

            ExpectingComma,

            InEscapedCharacter

        };

 

        public static string[] CsvSplit(this String source)

        {

            List<string> splitString = new List<string>();

            List<int> slashesToRemove = null;

            State state = State.AtBeginningOfToken;

            char[] sourceCharArray = source.ToCharArray();

            int tokenStart = 0;

            int len = sourceCharArray.Length;

            for (int i = 0; i <>

            {

                switch (state)

                {

                    case State.AtBeginningOfToken:

                        if (sourceCharArray[i] == '"')

                        {

                            state = State.InQuotedToken;

                            slashesToRemove = new List<int>();

                            continue;

                        }

                        if (sourceCharArray[i] == ',')

                        {

                            splitString.Add("");

                            tokenStart = i + 1;

                            continue;

                        }

                        state = State.InNonQuotedToken;

                        continue;

                    case State.InNonQuotedToken:

                        if (sourceCharArray[i] == ',')

                        {

                            splitString.Add(

                                source.Substring(tokenStart, i - tokenStart));

                            state = State.AtBeginningOfToken;

                            tokenStart = i + 1;

                        }

                        continue;

                    case State.InQuotedToken:

                        if (sourceCharArray[i] == '"')

                        {

                            state = State.ExpectingComma;

                            continue;

                        }

                        if (sourceCharArray[i] == '\\')

                        {

                            state = State.InEscapedCharacter;

                            slashesToRemove.Add(i - tokenStart);

                            continue;

                        }

                        continue;

                    case State.ExpectingComma:

                        if (sourceCharArray[i] != ',')

                            throw new CsvParseException("Expecting comma");

                        string stringWithSlashes =

                            source.Substring(tokenStart, i - tokenStart);

                        foreach (int item in slashesToRemove.Reverse<int>())

                            stringWithSlashes =

                                stringWithSlashes.Remove(item, 1);

                        splitString.Add(

                            stringWithSlashes.Substring(1,

                                stringWithSlashes.Length - 2));

                        state = State.AtBeginningOfToken;

                        tokenStart = i + 1;

                        continue;

                    case State.InEscapedCharacter:

                        state = State.InQuotedToken;

                        continue;

                }

            }

            switch (state)

            {

                case State.AtBeginningOfToken:

                    splitString.Add("");

                    return splitString.ToArray();

                case State.InNonQuotedToken:

                    splitString.Add(

                        source.Substring(tokenStart,

                            source.Length - tokenStart));

                    return splitString.ToArray();

                case State.InQuotedToken:

                    throw new CsvParseException("Expecting ending quote");

                case State.ExpectingComma:

                    string stringWithSlashes =

                        source.Substring(tokenStart, source.Length - tokenStart);

                    foreach (int item in slashesToRemove.Reverse<int>())

                        stringWithSlashes = stringWithSlashes.Remove(item, 1);

                    splitString.Add(

                        stringWithSlashes.Substring(1,

                            stringWithSlashes.Length - 2));

                    return splitString.ToArray();

                case State.InEscapedCharacter:

                    throw new CsvParseException("Expecting escaped character");

            }

            throw new CsvParseException("Unexpected error");

        }

    }

 

 

    class Program

    {

        static bool Validate(string[] results, string[] expectedResults)

        {

            if (results.Length != expectedResults.Length)

            {

                Console.WriteLine("  Validation error");

                return false;

            }

            for (int i = 0; i <>

            {

                if (results[i] != expectedResults[i])

                {

                    Console.WriteLine("  Validation error");

                    return false;

                }

            }

            Console.WriteLine("  Validated");

            return true;

        }

 

 

        static void ValidateAll()

        {

            string[] split;

 

            Console.WriteLine("Test1");

            split = "\"12\\\"3\",\"456\",\"789\"".CsvSplit();

            Validate(split, new[] { "12\"3", "456", "789" });

 

            Console.WriteLine("Test2");

            split = "\"123\",\"456\",\"789\"".CsvSplit();

            Validate(split, new[] { "123", "456", "789" });

 

            Console.WriteLine("Test3");

            split = "\"aaa,bbb\",\"ccc,ddd\",ghi".CsvSplit();

            Validate(split, new[] { "aaa,bbb", "ccc,ddd", "ghi" });

 

            Console.WriteLine("Test4");

            split = "aaa,,bbb".CsvSplit();

            Validate(split, new[] { "aaa", "", "bbb" });

 

            Console.WriteLine("Test5");

            try

            {

                split = "\"aaa\\bbb\",ccc,ddd".CsvSplit();

                Console.WriteLine("  Validation error");

            }

            catch (CsvParseException)

            {

                Console.WriteLine("  Validated");

            }

 

            Console.WriteLine("Test6");

            try

            {

                split = "\"aaabbb\"bbb,ccc,ddd".CsvSplit();

                Console.WriteLine("  Validation error");

            }

            catch (CsvParseException)

            {

                Console.WriteLine("  Validated");

            }

 

            Console.WriteLine("Test7");

            split = "aaa,,bbb,".CsvSplit();

            Validate(split, new[] { "aaa", "", "bbb", "" });

 

            Console.WriteLine("Test8");

            try

            {

                split = "\"aaabbb\",ccc,\"ddd".CsvSplit();

                Console.WriteLine("  Validation error");

            }

            catch (CsvParseException)

            {

                Console.WriteLine("  Validated");

            }

 

            Console.WriteLine("Test9");

            try

            {

                split = "aaa,ccc,\"ddd\\".CsvSplit();

                Console.WriteLine("  Validation error");

            }

            catch (CsvParseException)

            {

                Console.WriteLine("  Validated");

            }

 

            Console.WriteLine("Test10");

            split = "\"a\\\\aa\",,bbb,".CsvSplit();

            Validate(split, new[] { "a\\aa", "", "bbb", "" });

 

            Console.WriteLine("Test11");

            split = "\"a\\aa\",,bbb,".CsvSplit();

            Validate(split, new[] { "aaa", "", "bbb", "" });

        }

 

        ///

        /// Main Function

        ///

        /// Command Line arguments

        static void Main(string[] args)

        {

            XElement xmlDoc = new XElement("Root",

                File.ReadAllLines("TextFile.txt")

                    .Select

                    (

                        line =>

                        {

                            var split = line.CsvSplit();

                            return new XElement("Quote",

                                new XElement("Person", split[0]),

                                new XElement("Text", split[1])

                            );

                        }

                    )

            );

            Console.WriteLine(xmlDoc);

            ValidateAll();

        }

    }

}

Reference :- http://blogs.msdn.com/ericwhite/archive/2008/09/30/linq-to-text-and-linq-to-csv.aspx

Posted By :- EricWhite