Archive for the ‘C# (C-Sharp)’ Category

6
Nov

Convert XML to CSV using LINQ to XML

   Posted by: Rasik Jain Tags: , , ,

This post will show you how to convert an XML file to CSV using Linq to XML and Anonymous types of C# 3.0. LINQ to XML is an lightweight in-memory XML programming API by which a user can manipulate the XML data such as

  • Reading from XML file
  • Writing to XML file
  • Add or Remove XML elements from XML tree.

Now lets dig into the code sample.

Following is the code snippet which helps in converting the XML file into CSV Format.

// Loading from a file, you can also load from a stream
XDocument loaded = XDocument.Load(@"C:\Contacts.xml");
 
// create a writer and open the file
TextWriter tw = new StreamWriter("c:\\XmltoCSV.txt");
 
// Query the data and write out a subset of contacts
var contacts = from c in loaded.Descendants("CONTACT")
		select new
		{
			FirstName = (string)c.Element("FIRSTNAME"),
			LastName = (string)c.Element("LASTNAME"),
			City = (string)c.Element("CITY"),
			State = (string)c.Element("STATE"),
			Country = (string)c.Element("COUNTRY")
		};
 
foreach (var contact in contacts)
{
	string s = contact.FirstName + "," +
				contact.LastName+ "," +
				contact.City+ "," +
				contact.State + "," +
				contact.Country;
 
	// write a line of text to the file
	tw.WriteLine(s);
}
 
// close the stream
tw.Close();

INPUT (XML File): We will use the XML file to read the data.

<?xml version='1.0' encoding='utf-8'?>
<CONTACTS>
	<CONTACT>
		<FIRSTNAME>John</FIRSTNAME>
		<LASTNAME>Doe</LASTNAME>	
		<CITY>Miami</CITY>
		<STATE>Florida</STATE>
		<COUNTRY>USA</COUNTRY>
	</CONTACT>
	<CONTACT>
		<FIRSTNAME>Shane</FIRSTNAME>
		<LASTNAME>Warne</LASTNAME>	
		<CITY>Sydney</CITY>
		<STATE>New South Wales</STATE>
		<COUNTRY>Australia</COUNTRY>
	</CONTACT>
	<CONTACT>
		<FIRSTNAME>Adam</FIRSTNAME>
		<LASTNAME>Smith</LASTNAME>	
		<CITY>London</CITY>
		<STATE>London</STATE>
		<COUNTRY>UK</COUNTRY>
	</CONTACT>
</CONTACTS>

OUTPUT (CSV file): Comma Separated File (CSV) will be generated as the output.

John,Doe,Miami,Florida,USA
Shane,Warne,Sydney,New South Wales,Australia
Adam,Smith,London,London,UK
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Reddit
  • HackerNews
  • Slashdot
  • StumbleUpon
  • TwitThis
  • Yahoo! Buzz
  • Tumblr
  • Share/Bookmark
6
May

.NET (C# or VB.Net) libraries for Twitter API

   Posted by: Rasik Jain Tags: , , , ,

I was looking for Twitter APIs or Wrapper classes which are built using .NET languages like C# or VB.NET. I have found some very good .NET libraries which are worth checking out them. If you are a .NET developer you can plugin these API libraries directly into your ASP.NET or Windows application without having to re-write everything from scratch in order to interact with twitter.

I have explored following APIs.

TweetSharp API

TweetSharp API is built using the .NET framework 3.0/3.5. It uses most of new features of C# like Linq, Implicit Variables, Extension Methods etc. TweetSharp can be used with silver light. Find more details about TweetSharp at Google Code – TweetSharp

You can interact with twitter as shown the below.

var twitter = FluentTwitter.CreateRequest()
    .AuthenticateAs("UserName", "Password")
    .Statuses().Update("Hello World")
    .AsJson();
 
var response = twitter.Request();

Twitterizer

Twitterizer is another .NET library which is promising. Already there are applications built using twitterizer. This Library provides simple to use functions and returns the user objects in the form of Collection Objects. Find more details at Google Code – Twitterizer

You can interact with twitter as shown the below.

Twitter tw = new Twitter("UserName", "Password");
 
tw.Status.Update("Hello World");

Yedda Lib

Yedda Lib is built using .NET 2.0 and C#. This is a simple and yet power library to use. When I checked this library last time there was no support for Direct Messages functionality. You can check more details at Yedda

Twitter tw = new Twitter();
 
//Using String object
String jsonFriends = tw.GetFriendsAsJSON("UserName", "Password");
 
//Using XML Object
XmlDocument xmlFriends = tw.GetFriendsAsXML("UserName", "Password");

TwitterVB

TwitterVB is a VB.NET library released as open source under Google Code. TwitterVB includes a working implementation of Twitter’s OAuth authentication system, as well as Basic Authentication. Find more details about TwitterVB at Google Code – TwitterVB

Dim tw As New TwitterVB.Twitter
tw = New TwitterVB.Twitter
tw.AuthenticateAs("UserName", "Password")
 
' Post the tweet to Twitter
tw.StatusMethods.Update(Me.txtPost.Text)
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Reddit
  • HackerNews
  • Slashdot
  • StumbleUpon
  • TwitThis
  • Yahoo! Buzz
  • Tumblr
  • Share/Bookmark