A Place for C Sharpers/.Netters

I Will do coding till last moment of life-Kiran Patil

Archive for the ‘LINQ’ Category

Convert XDocument to GZip format

Posted by kiranpatils on September 25, 2008

Here is the method which will help you..

/// <summary>
/// This function will be used to Compress XML DATA
/// using Gzip Stream
/// </summary>
/// <param name=”xDocument”>XMLDocument to compress</param>
/// <returns>byte array</returns>
private byte[] ConvertXDocumentToGzip(XDocument xDocument)
{
//buffer to write Compressed data
byte[] buffer;

//Compress the XML DATA
MemoryStream memoryStream = new MemoryStream();
XmlWriter xmlWriter = XmlWriter.Create(memoryStream);

//Save data to memoryStream
xDocument.Save(xmlWriter);

//writer Close
xmlWriter.Close();

//Reset Memorystream postion to 0
memoryStream.Position = 0;

//create buffer of total content Length
buffer = new byte[memoryStream.Length];

//Create a stream to compress the data
GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress);
//compress the data
gzipStream.Write(buffer, 0, buffer.Length);

//Close the underlying streams
gzipStream.Flush();
gzipStream.Close();

memoryStream.Flush();
memoryStream.Close();

//return compressed data as byte array
return buffer;
}

Posted in LINQ | Tagged: | Leave a Comment »

Generic List to XML using LINQ

Posted by kiranpatils on September 23, 2008

Let us see  how to generate list of XML using LINQ.
 
Scenario
I have one generic List of Employee and want to convert it in to XML format.
 
Solution:
I can do this using XmlDocument class which is .NET 2.0 but it can take more than 30-40 lines..and i want to achieve[achieved also!!]
it in 4-5 Lines…sounds cool idea..so lets see how can we achieve it using LINQ–Language integrated Query new poweful tool of Micro.
 
First i have this Simple Class for storing Employee value:
 
Employee.CS
——————-
using System;
namespace LinqtoXMLDemo
{
 public class Employee
 {
  public int EmployeeID
  {
   get;
   set;
  }
  public string EmployeeName
  {
   get;
   set;
  }
  public string EmployeeAddress
  {
   get;
   set;
  }
 }
}
 
Ok Lets start
 
1. Add some sample values in list
List<Employee> employeeList = new List<Employee>();
      
   employeeList.Add(new Employee()
   {
    EmployeeAddress=”BARODA”,
    EmployeeID=1,
    EmployeeName=”XYZ”    
   });
   employeeList.Add(new Employee()
   {
    EmployeeAddress = “Bangalore”,
    EmployeeID = 2,
    EmployeeName = “ABC”
   });
 
2. Now i want to convert it into XML format..which should look like this..
<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>
<Employees>
 <Employee>
  <EmployeeID>1</EmployeeID>
  <EmployeeName>XYZ</EmployeeName>
  <EmployeeAddress>BARODA</EmployeeAddress>
 </Employee>
 <Employee>
  <EmployeeID>2</EmployeeID>
  <EmployeeName>ABC</EmployeeName>
  <EmployeeAddress>Bangalore</EmployeeAddress>
 </Employee>
</Employees>
 
3. Method to achieve our desire..[Which is self explanatory)
/// <summary>
  /// This Method will generate list TO XML using LINQ
  /// </summary>
  /// <param name=”employeeList”>Collection of Employe</param>
  /// <returns>XmlDocument</returns>
  private static XDocument GenerateListToXML(List<Employee> employeeList)
  {
   XDocument xmlDocument = new XDocument(new XDeclaration(“1.0″, “UTF-8″, “yes”),
              new XElement(“Employees”,
              from employee in employeeList
              select new XElement(“Employee”,
              new XElement(“EmployeeID”, employee.EmployeeID),
                  new XElement(“EmployeeName”, employee.EmployeeName),
                   new XElement(“EmployeeAddress”, employee.EmployeeAddress))));
   return xmlDocument;
  }
 
4. From main call it like this:
 
XDocument employeeXML = GenerateListToXML(employeeList);
That’s it we are ready!!!!
Okay XML Looks cool..But now i want it with namespace so it should look like this..
<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>
<Employees xmlns=”
http://www.abc.org/employees/0.9“>
 <Employee>
  <EmployeeID>1</EmployeeID>
  <EmployeeName>XYZ</EmployeeName>
  <EmployeeAddress>BARODA</EmployeeAddress>
 </Employee>
 <Employee>
  <EmployeeID>2</EmployeeID>
  <EmployeeName>ABC</EmployeeName>
  <EmployeeAddress>Bangalore</EmployeeAddress>
 </Employee>
</Employees>
Method to generate XML with Namespace
/// <summary>
  /// This Method will generate list TO XML with namespace using LINQ
  /// </summary>
  /// <param name=”employeeList”>Collection of Employe</param>
  /// <returns>XmlDocument</returns>
  private static XDocument GenerateListToXMLWithNameSpace(List<Employee> employeeList)
  {
   XNamespace employeeNameSpace = “
http://www.abc.org/employees/0.9“;
   XDocument xmlDocument = new XDocument(new XDeclaration(“1.0″, “UTF-8″, “yes”),
              new XElement(employeeNameSpace + “Employees”,
              from employee in employeeList
              select new XElement(employeeNameSpace + “Employee”,
              new XElement(employeeNameSpace + “EmployeeID”, employee.EmployeeID),
                  new XElement(employeeNameSpace + “EmployeeName”, employee.EmployeeName),
                   new XElement(employeeNameSpace + “EmployeeAddress”, employee.EmployeeAddress))));
   return xmlDocument;
  }
 
That’s it…Happy LINQ!!
 
 

Posted in LINQ | Tagged: | Leave a Comment »