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.

Wednesday, September 10, 2008

Syntax of Path.Data

When we create drawing Paths in blend it gives us the Data in a special format with some letters which is not easily understandable at first look.
Here is a link which describes how the control letters M ,C, L etc are getting mapped.
http://msdn.microsoft.com/en-us/library/ms747393.aspx#paths

One more link to create paths simply using individual objects.
http://msdn.microsoft.com/en-us/library/ms745814.aspx

Wednesday, July 2, 2008

About pack uri style

As a WPF developer you might have seen the resource reference in the form

pack://application:,,,/somefile.xaml

and you might have used this style too.Actually what is this pack uri ?


This is a new method introduced in WPF to locate files in an assembly or in the current folder.Those files may be images,videos,audio and even another xaml file.

Parts

The first part identifies whether it points to an application or to the current folder where the file is located and the second part is the relative path to that file.

First part can be any one of the following

  • application -> Tells that the file is located in the assembly as Content or Resource
  • pack://application:,,,/ResourceFile.xaml
  • siteoforigin -> Its in the current executing folder
  • pack://siteoforigin:,,,/SiteOfOriginFile.xaml

      Second part tells the assembly where the file is located.If the file locate in same assembly there is no need to mention it other wise use assembly name

      Same assembly - pack://application:,,,/ResourceFile.xaml
      Different assembly - pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

      Third part is the version and this is optional

      pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml

      Fourth part is the path to file.

      pack://application:,,,/Subfolder/ResourceFile.xaml

      Advantage of pack uri style

      • You may separate your styles from application and keep as dll.
      • Design time support
      What msdn says