This blog is moved to
http://amalhashim.wordpress.com

Wednesday, May 18, 2011

Serialize Multiobject ArrayList to XML

This is a code snippet which shows how to Serialize an ArrayList to XML. ArrayList contains objects of different types.

using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
AListWrapper wrapper = new AListWrapper();

XmlSerializer mySerializer = new XmlSerializer(typeof(AListWrapper));

StreamWriter myWriter = new StreamWriter("c:\\myFileName.xml");

mySerializer.Serialize(myWriter, wrapper);
myWriter.Close();
}
}

public class Animal
{
public string Type { get; set; }
public int Age { get; set; }
}

public class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
public string Address { get; set; }
}

[XmlRoot("ArrayList")]
public class AListWrapper
{
[XmlElement(Type = typeof(Employee)),
XmlElement(Type = typeof(Animal))]
public ArrayList list = new ArrayList();

public AListWrapper()
{
Animal animal = new Animal()
{
Age = 1,
Type = "Dog"
};

Employee emp = new Employee()
{
Address = "Address",
Name = "SomeName",
Salary = 2000.50
};

list.Add(animal);
list.Add(emp);
}
}
}

Wednesday, May 4, 2011

jQuery 1.6 Released

Sams Teach Yourself jQuery in 24 Hours
jQuery 1.6 is now available for download!

jQuery CDN:
Microsoft Ajax CDN:
You can read more about this on the following link: http://blog.jquery.com/2011/05/03/jquery-16-released/

Monday, May 2, 2011

SharePoint 2010 Word Automation Services

Professional SharePoint 2010 AdministrationaltBeginning SharePoint 2010 Development (Wrox Beginning Guides)Microsoft SharePoint 2010: Building Solutions for SharePoint 2010 (Books for Professionals by Professionals)altMicrosoft® SharePoint® 2010 Administrator's Companionalt

In SharePoint 2010, you can add "Word Automation Services" from Central Admin as shown below

image

Once the service is in place, we can easily convert Word documents to PDF as shown in below snippet

private voidConvertDocFileToPDF(SPFile filex)
{           
    ConversionJobSettings settings = newConversionJobSettings();
    settings.OutputFormat = SaveFormat.PDF;
    ConversionJob job = newConversionJob(ConfigurationHelper.Instance.DocumentConversionService, settings);   
    job.UserToken = SPUserToken.SystemAccount;
    stringpdfFile = filex.Url.Replace("docx", "pdf");
    job.AddFile("http://<your site URL>/"+ filex.Url, "http://<your site URL>/"+ pdfFile);
    job.Start();
}

Ensure to add reference to “Microsoft.Office.Word.Server” present under 14 hive “ISAPI” folder.

To test this, run “Word Automation Services Timer Job”

DataTable to GenericList | C# and LINQ

Programming Microsoft® LINQ in Microsoft .NET Framework 4 
using System;
using System.Data;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
DataTable table = new DataTable
{
Columns = {
{"Id", typeof(int)},
{"Name", typeof(string)}
}
};

table.Rows.Add(1, "Amal");
table.Rows.Add(1, "Fousiya");
table.Rows.Add(1, "Munna");
table.Rows.Add(1, "Hussain");         

var listOfEmployees = from row in table.AsEnumerable()
select new Employee
{
Id = row.Field<int>("Id"),
Name = row.Field<String>("Name")
};

foreach (Employee emp in listOfEmployees)
{
Console.WriteLine("{0}   {1}", emp.Id, emp.Name);
}
}
}

class Employee
{
public int Id { get; set; }
public String Name { get; set; }
}
}