Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Northwind & Photos - How Many Times Will I Write This Code?

Blogs

Mike Taulty's Blog

Elsewhere

Archives

Not the first time I've done this (at least LINQ makes it a bit more interesting);

  static void Main(string[] args)
  {
    using (NorthwindDataContext ctx = new NorthwindDataContext())
    {
      List<Employee> employees = ctx.Employees.ToList();

      XElement photos =
        new XElement("photos",
          from e in employees
          select new XElement("employee",
            new XAttribute("id", e.EmployeeID),
            new XAttribute("photo", MakeFileName(e.EmployeeID))));

      photos.Save("photos.xml");

      foreach (Employee e in employees)
      {
        // bit wasteful...
        byte[] bytes = new byte[e.Photo.Length - 78];
        Array.Copy(e.Photo, 78, bytes, 0, e.Photo.Length - 78);

        File.WriteAllBytes(MakeFileName(e.EmployeeID), bytes);
      }
    }
  }
  static string MakeFileName(int id)
  {
    return (string.Format(".\\images\\{0}.bmp", id));
  }

Posted Mon, Sep 10 2007 3:59 AM by mtaulty

Comments

Christopher Steen wrote Link Listing - September 10, 2007
on Mon, Sep 10 2007 8:35 PM
Link Listing - September 10, 2007