Sunday, December 14, 2008

Xml to Object model using linq

Have you thought of loading xml into an object model by single line of code?
It is possible with the help of linq.Just you need to write a single statement to obtain xml 2 class object model conversion.
This post needs some basic understanding about Linq.You may get it by visiting this post.

Here is the Xml you are going to convert.
<?xml version="1.0" encoding="utf-8" ?>
<
Employees>
<
Employee ID="10">
<
Name>Joy</Name>
<
Designation>Sr Software Engg</Designation>
</
Employee>
<
Employee ID="12">
<
Name>ABC</Name>
<
Designation>Software Engg</Designation>
</
Employee>
<
Employee ID="1">
<
Name>George</Name>
<
Designation>Sr Software Engg</Designation>
</
Employee>
<
Employee ID="2">
<
Name>IJK</Name>
<
Designation>Testing Engg</Designation>
</
Employee>
<
Employee ID="3">
<
Name>XYZ</Name>
<
Designation>Team Leader</Designation>
</
Employee>
</
Employees>
The Employee class is given below
public class Employee
{
public string Name { get; set; }
public string Designation { get; set; }
}
The first step here is to load the xml into a XmlDocument.Then apply Linq.
Here goes the Linq Query.
XmlDocument xmldocument = new XmlDocument();
xmldocument.Load("../../employees.xml");
List<Employee> emps = (from i 
                       in xmldocument.ChildNodes[1].ChildNodes.Cast<XmlNode>()
                       select new Employee 
                       { 
                           Name = i.ChildNodes[0].InnerText ,
                           Designation = i.ChildNodes[1].InnerText 
                       }
                       ).ToList();

Download sample from here

Saturday, December 13, 2008

Creating custom wpf markup extension to show class fields

Here is one link which describes what is markup extension and available markup extensions.
Markup extensions are very much useful to obtain a clean project.Things won't get mixed with each other.Xaml itself contains the lines which have the functionality.

Consider the scenario where we need to display all properties of given class in a listbox.We normally write C# or VB code to get PropertyInfo objects and return the same.This code normally resides in presentation files itself.

Here comes the need for markup extension.We can write our own extension by inheriting from the MarkUpExtension class.

Steps to Create a custom markup extension

  1. Create a class which derives from MarkupExtension.The naming convention is that the class name should ends with 'Extension' .Eg:TypeDescriptorExtension
  2. If we need to accept a default parameter with extension write a constructor
  3. Add other properties as well.
  4. Override ProvideValue Method and return the appropriate value after processing.

Eg: Creating custom markup extension to list Properties and methods of a Class

The PropertyDescriptorExtension class is derived from Markupextension and it accepts an object Type as default parameter.So the constructor is written with a parameter of type 'Type'.

public class TypeDescriptorExtension:MarkupExtension
{
public TypeDescriptorExtension( Type type):this()
{
Type = type;
}
}

The other properties are IncludeProperties and InCludeMethods which are boolean to indicate whether the out put list should include Properties and Methods respectively.IncludeProperties is true by default.

Now override the ProvideValue method.Get the properties and methods collection based on the properties using Reflection and return as well.

public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Type == null)
{
throw new InvalidOperationException("The Class must be specified.");
}
List<string> items = new List<string>();

if (IncludeProperties)
{
foreach (PropertyInfo item in Type.GetProperties())
{
items.Add(item.ToString());
}
}
if (IncludeMethods)
{
foreach (MethodInfo info in Type.GetMethods())
{
items.Add(info.ToString());
}
}
return items;
}

Using the TypeDescriptorExtension
Normal, which displays only properties

 <ListBox ItemsSource="{Binding Source={local:TypeDescriptor {x:Type Button}}}" />
Displays methods only
 <ListBox ItemsSource="{Binding Source={local:TypeDescriptor {x:Type Button},IncludeMethods=True,IncludeProperties=False}}" />

Sample is available here.