Blog

Sep 13
Xml Serialization of collection without root element
 
   Xml Serialization is process of converting an object into XML form. It is a very handy feature if you use XMLs for configurations or for exchange of message between systems. This blog post explains How to serialize a collection object without root element ?
 
   Consider the following scenario, where we are trying to serialize a flight object with collection of bags.

 
Flight flight = new Flight();
flight.FlightNumber = "BA 001";
flight.Bags.Add(new Bag { TagNumber = "0999123000", Weight = "10.5", PassengerName = "Alpha" });
flight.Bags.Add(new Bag { TagNumber = "0999123001", Weight = "8.0", PassengerName = "Beta" });
Serialize(flight);

 

The class definitions are 

 public class Flight
{
    public string FlightNumber { get; set; }
    public List<Bag> Bags      { get; set; }        
}

public class Bag
{
    public string TagNumber     { get; set; }
    public string Weight        { get; set; }
    public string PassengerName { get;set; }      
}

Note we have not used any Serialization control attributes hence by default class and properties are serialized as XmlElement.For the above scenario the XML generated will be
 
<?xml version="1.0" encoding="utf-16"?>
<Flight xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FlightNumber>BA 001</FlightNumber>
  <Bags>
    <Bag>
      <TagNumber>0999123000</TagNumber>
      <Weight>10.5</Weight>
      <PassengerName>Alpha</PassengerName>
     </Bag>
    <Bag>
      <TagNumber>0999123001</TagNumber>
      <Weight>8.0</Weight>
      <PassengerName>Beta</PassengerName>
    </Bag>
 </Bags>
</Flight>
 
Note that items in Bags collection is serialized within root element <Bags>. In some scenarios you may not need the root element.But   
how to serialize items in collection without the root element ?

Well, you can remove it by defining class with XmlElement Attribute to Collection property.
 
public class Flight
  public string FlightNumber { get; set; }
 
 [XmlElement("BagInFlight")]
  public List<Bag> Bags { get; set; }
}

Adding XmlElement attribute will transform the serialized xml into
 
<?xml version="1.0" encoding="utf-16"?>
<Flight xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FlightNumber>BA 001</FlightNumber>
  <BagInFlight>
    <TagNumber>0999123000</TagNumber>
    <Weight>10.5</Weight>
    <PassengerName>Alpha</PassengerName>
  </BagInFlight>
  <BagInFlight>
    <TagNumber>0999123001</TagNumber>
    <Weight>8.0</Weight>
    <PassengerName>Beta</PassengerName>
  </BagInFlight>
</Flight>        

 
Note that the root element is no longer present in serializes xml output
Also note that the name of Bag element is changed to "BagInFlight" which we specified in attribute.Hope this tip helps.​
Jan 09
Online Placeholder Image Services
​​

​ While designing websites, you may need images of different dimensions. Often images of right dimensions may not be available at the time of design. In such scenarios you can  use online place holder image services with minimal effort.These services provide random images of specified dimension to be used as placeholder.​

There are many innovative services available.My favoutites are

Placehold.it 

Usage

​To get an image just put your image dimesion on url

e.g: <img src= "http://placehold.it/250X200" />  will give the image

Image of dimension 250X200

For more options visit  Placehold.it site

flickholdr.com

flickerholder.com provides Flicker photos of specified dimension as placeholder

Usage​​​ 

​​<img src="http://flickholdr.com/200/300" />

FlickerHolder Image

There are more options available visit flickholdr.com

Hope this post is useful.

Dec 04
Welcome to CodeReflex Technologies blog!

​Hello World :)