StumbleUpon
Share on Facebook

Thursday, January 29, 2009

How to Convert CSV to XML using C#

C# CSV to XML Conversion, CSV to XML Conversion using LINQ


Language Integrated Query (LINQ) can be used to convert a CSV file to XML. Here is the sample CSV file that needs to be converted

The following code reads the CSV file to an array. LINQ is used to loop through the array and the contents are written as XML using XElement (System.XML.Linq).


public void ConvertCSVToXML()

{

String[] FileContent = File.ReadAllLines(@"C:\Temp\vba.csv");

String XMLNS = "";

XElement Inv = new XElement("Invoice",

from items in FileContent

let fields = items.Split(',')

select new XElement("Item",

new XElement("ID", fields[0]),

new XElement("Name", fields[1]),

new XElement("Price", fields[2]),

new XElement("Availability", fields[3]),

new XElement("TotalPrice", fields[4])

)

);

File.WriteAllText(@"C:\Temp\vba.xml", XMLNS + Inv.ToString() );

}

Here is the Input Text File


The output XML looks like


LINQ and C#, C# Convert CSV To XML, .NET CSV To XML, LINQ CSV to XML, Convert Text files to XML using C#,
StumbleUpon
Share on Facebook

List files based on FileSize using C#

C# Get Files Greater than Specific Size using Linq


C# and LINQ can be combined to do wonders; here is a sample of that. The following snippet extracts the files that are more thsn 345 KB

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.Length > 340000

select FI.FullName + " " + FI.Length.ToString() ;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

Extract files based on Size in C#, Extract files greater than 1GB using C#, C# List files based on Size, Filter by FileSize using C#, Extract files based on Size in .NET, Extract files greater than 1GB using .NET, .NET List files based on Size,

StumbleUpon
Share on Facebook

C# GetFiles with Date Filter

How to Filter files that are modified in a specific day using C#

Here is a snippet using C# and LINQ, which will retrieve the files from a directory for a specific date

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.LastWriteTime.Date == DateTime.Now.Date

select FI.FullName + " " + FI.LastWriteTime;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}



StumbleUpon
Share on Facebook

C# Get Read Only Files

List all ReadOnly files in a directory using C# (.NET)

The following code will retrieve the read-only files using LINQ and C#

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.IsReadOnly == true

select FI.FullName + " " + FI.LastWriteTime;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

The above can (and mostly is) done by the following way

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

foreach (FileInfo F1 in FileList)

{

if (F1.IsReadOnly == true)

{

Console.WriteLine(F1.FullName );

}

}

List ReadOnly files using C#, Retrieve Read-Only files using C#, Get Read Only Files using C#, List ReadOnly files using .NET, Retrieve Read-Only files using .NET, Get Read Only Files using .NET

StumbleUpon
Share on Facebook

Essential guide to learn what you need to do before implementing SQL Server 2008.

Are you ready to deploy SQL Server 2008?

Many IT departments considering a migration to SQL Server 2008 are anxious about what may happen if the server does not perform well at deployment. This essential guide explains how analyzing your disk, memory, and processor requirements can help you develop a migration plan that will make your deployment an immediate success.

Download this essential guide to learn what you need to do before implementing SQL Server 2008.
Related Posts Plugin for WordPress, Blogger...