LIN{UX,Q} : It's time for a refreshment(3)

Setelah membandingkan bagaimana meng-query sekumpulan object dari sebuah object pada artikel sebelumnya, pada bagian ini, kita akan melihat bagaimana LINQ memudahkan kita mengquery object dari sebuah file XML dan bagaimana menyusun sebuah file XML.

Misalkan kita ingin meng-query oject dari sebuah file Customers.xml yang isinya seperti ini:

   1: <?xml version="1.0" encoding="ISO-8859-1" ?> 
   2: <Customers>
   3: <Customer CustomerID="ALFKI" ContactName="Maria Anders" City="Berlin" />
   4: <Customer CustomerID="ANATR" ContactName="Ana Trujillo" City="México D.F." />
   5: <Customer CustomerID="ANTON" ContactName="Antonio Moreno" City="México D.F." />
   6: <Customer CustomerID="AROUT" ContactName="Thomas Hardy" City="London" />
   7: <Customer CustomerID="BERGS" ContactName="Christina Berglund" City="Luleå" />
   8: <Customer CustomerID="BLAUS" ContactName="Hanna Moos" City="Mannheim" />
   9: <Customer CustomerID="BLONP" ContactName="Frédérique Citeaux" City="Strasbourg" />
  10: <Customer CustomerID="BOLID" ContactName="Martín Sommer" City="Madrid" />
  11: <Customer CustomerID="BONAP" ContactName="Laurence Lebihan" City="Marseille" />
  12: <Customer CustomerID="BOTTM" ContactName="Elizabeth Lincoln" City="Tsawassen" />
  13: <Customer CustomerID="BSBEV" ContactName="Victoria Ashworth" City="London" />
  14: <Customer CustomerID="CACTU" ContactName="Patricio Simpson" City="Buenos Aires" />
  15: <Customer CustomerID="CENTC" ContactName="Francisco Chang" City="México D.F." />
  16: <Customer CustomerID="CHOPS" ContactName="Yang Wang" City="Bern" />
  17: <Customer CustomerID="COMMI" ContactName="Pedro Afonso" City="Sao Paulo" />
  18: <Customer CustomerID="CONSH" ContactName="Elizabeth Brown" City="London" />
  19: <Customer CustomerID="DRACD" ContactName="Sven Ottlieb" City="Aachen" />
  20: <Customer CustomerID="DUMON" ContactName="Janine Labrune" City="Nantes" />
  21: <Customer CustomerID="EASTC" ContactName="Ann Devon" City="London" />
  22: <Customer CustomerID="ERNSH" ContactName="Roland Mendel" City="Graz" />
  23: <Customer CustomerID="FAMIA" ContactName="Aria Cruz" City="Sao Paulo" />
  24: <Customer CustomerID="FISSA" ContactName="Diego Roel" City="Madrid" />
  25: <Customer CustomerID="FOLIG" ContactName="Martine Rancé" City="Lille" />
  26: <Customer CustomerID="FOLKO" ContactName="Maria Larsson" City="Bräcke" />
  27: <Customer CustomerID="FRANK" ContactName="Peter Franken" City="München" />
  28: <Customer CustomerID="FRANR" ContactName="Carine Schmitt" City="Nantes" />
  29: <Customer CustomerID="FRANS" ContactName="Paolo Accorti" City="Torino" />
  30: <Customer CustomerID="FURIB" ContactName="Lino Rodriguez" City="Lisboa" />
  31: <Customer CustomerID="GALED" ContactName="Eduardo Saavedra" City="Barcelona" />
  32: <Customer CustomerID="GODOS" ContactName="José Pedro Freyre" City="Sevilla" />
  33: <Customer CustomerID="GOURL" ContactName="André Fonseca" City="Campinas" />
  34: <Customer CustomerID="GREAL" ContactName="Howard Snyder" City="Eugene" />
  35: <Customer CustomerID="GROSR" ContactName="Manuel Pereira" City="Caracas" />
  36: <Customer CustomerID="HANAR" ContactName="Mario Pontes" City="Rio de Janeiro" />
  37: <Customer CustomerID="HILAA" ContactName="Carlos Hernández" City="San Cristóbal" />
  38: <Customer CustomerID="HUNGC" ContactName="Yoshi Latimer" City="Elgin" />
  39: <Customer CustomerID="HUNGO" ContactName="Patricia McKenna" City="Cork" />
  40: <Customer CustomerID="ISLAT" ContactName="Helen Bennett" City="Cowes" />
  41: <Customer CustomerID="KOENE" ContactName="Philip Cramer" City="Brandenburg" />
  42: <Customer CustomerID="LACOR" ContactName="Daniel Tonini" City="Versailles" />
  43: <Customer CustomerID="LAMAI" ContactName="Annette Roulet" City="Toulouse" />
  44: <Customer CustomerID="LAUGB" ContactName="Yoshi Tannamuri" City="Vancouver" />
  45: <Customer CustomerID="LAZYK" ContactName="John Steel" City="Walla Walla" />
  46: <Customer CustomerID="LEHMS" ContactName="Renate Messner" City="Frankfurt a.M." />
  47: <Customer CustomerID="LETSS" ContactName="Jaime Yorres" City="San Francisco" />
  48: <Customer CustomerID="LILAS" ContactName="Carlos González" City="Barquisimeto" />
  49: <Customer CustomerID="LINOD" ContactName="Felipe Izquierdo" City="I. de Margarita" />
  50: <Customer CustomerID="LONEP" ContactName="Fran Wilson" City="Portland" />
  51: <Customer CustomerID="MAGAA" ContactName="Giovanni Rovelli" City="Bergamo" />
  52: <Customer CustomerID="MAISD" ContactName="Catherine Dewey" City="Bruxelles" />
  53: <Customer CustomerID="MEREP" ContactName="Jean Fresnière" City="Montréal" />
  54: <Customer CustomerID="MORGK" ContactName="Alexander Feuer" City="Leipzig" />
  55: <Customer CustomerID="NORTS" ContactName="Simon Crowther" City="London" />
  56: <Customer CustomerID="OCEAN" ContactName="Yvonne Moncada" City="Buenos Aires" />
  57: <Customer CustomerID="OLDWO" ContactName="Rene Phillips" City="Anchorage" />
  58: <Customer CustomerID="OTTIK" ContactName="Henriette Pfalzheim" City="Köln" />
  59: <Customer CustomerID="PARIS" ContactName="Marie Bertrand" City="Paris" />
  60: <Customer CustomerID="PERIC" ContactName="Guillermo Fernández" City="México D.F." />
  61: <Customer CustomerID="PICCO" ContactName="Georg Pipps" City="Salzburg" />
  62: <Customer CustomerID="PRINI" ContactName="Isabel de Castro" City="Lisboa" />
  63: <Customer CustomerID="QUEDE" ContactName="Bernardo Batista" City="Rio de Janeiro" />
  64: <Customer CustomerID="QUEEN" ContactName="Lúcia Carvalho" City="Sao Paulo" />
  65: <Customer CustomerID="QUICK" ContactName="Horst Kloss" City="Cunewalde" />
  66: <Customer CustomerID="RANCH" ContactName="Sergio Gutiérrez" City="Buenos Aires" />
  67: <Customer CustomerID="RATTC" ContactName="Paula Wilson" City="Albuquerque" />
  68: <Customer CustomerID="REGGC" ContactName="Maurizio Moroni" City="Reggio Emilia" />
  69: <Customer CustomerID="RICAR" ContactName="Janete Limeira" City="Rio de Janeiro" />
  70: <Customer CustomerID="RICSU" ContactName="Michael Holz" City="Genève" />
  71: <Customer CustomerID="ROMEY" ContactName="Alejandra Camino" City="Madrid" />
  72: <Customer CustomerID="SANTG" ContactName="Jonas Bergulfsen" City="Stavern" />
  73: <Customer CustomerID="SAVEA" ContactName="Jose Pavarotti" City="Boise" />
  74: <Customer CustomerID="SEVES" ContactName="Hari Kumar" City="London" />
  75: <Customer CustomerID="SIMOB" ContactName="Jytte Petersen" City="Kobenhavn" />
  76: <Customer CustomerID="SPECD" ContactName="Dominique Perrier" City="Paris" />
  77: <Customer CustomerID="SPLIR" ContactName="Art Braunschweiger" City="Lander" />
  78: <Customer CustomerID="SUPRD" ContactName="Pascale Cartrain" City="Charleroi" />
  79: <Customer CustomerID="THEBI" ContactName="Liz Nixon" City="Portland" />
  80: <Customer CustomerID="THECR" ContactName="Liu Wong" City="Butte" />
  81: <Customer CustomerID="TOMSP" ContactName="Karin Josephs" City="Münster" />
  82: <Customer CustomerID="TORTU" ContactName="Miguel Angel Paolino" City="México D.F." />
  83: <Customer CustomerID="TRADH" ContactName="Anabela Domingues" City="Sao Paulo" />
  84: <Customer CustomerID="TRAIH" ContactName="Helvetius Nagy" City="Kirkland" />
  85: <Customer CustomerID="VAFFE" ContactName="Palle Ibsen" City="Århus" />
  86: <Customer CustomerID="VICTE" ContactName="Mary Saveley" City="Lyon" />
  87: <Customer CustomerID="VINET" ContactName="Paul Henriot" City="Reims" />
  88: <Customer CustomerID="WANDK" ContactName="Rita Müller" City="Stuttgart" />
  89: <Customer CustomerID="WARTH" ContactName="Pirkko Koskitalo" City="Oulu" />
  90: <Customer CustomerID="WELLI" ContactName="Paula Parente" City="Resende" />
  91: <Customer CustomerID="WHITC" ContactName="Karl Jablonski" City="Seattle" />
  92: <Customer CustomerID="WILMK" ContactName="Matti Karttunen" City="Helsinki" />
  93: <Customer CustomerID="WOLZA" ContactName="Zbyszek Piestrzeniewicz" City="Warszawa" />
  94: </Customers>

 

Kita bisa meload file .xml tersebut dan menggunakan element yang kita inginkan dengan menggunakan XElement.Load(namafile).Elements("namaelement"), kemudian kita merekonstruksi object yang kita inginkan dengan menggunakan attribut yang sesuai yang diambil dari file xml yang diload,

   1: var custs = from c in XElement.Load("Customers.xml").Elements("Customer")
   2:                     select new Customer
   3:                     {
   4:                         City = c.Attribute("City").Value,
   5:                         ContactName = c.Attribute("ContactName").Value
   6:                     };

Di sini kita load file Customers.xml, kemudian merekonstruksi object Customer dengan properties City dan ContactName masing2 diambil dari attribute City dan ContactName dari element customer dari file xml tersebut, adapun attribut CustomerID di sini tidak akan kita gunakan.

Seperti pada artikel sebelumnya, selanjutnya kita bisa melalukan query terhadap object Customer untuk menghitung berapa jumlah Customer yang berasal dari setiap kota yang dimulai dengan huruf "M", dengan query berikut:

   1: var results = from c in custs
   2:                     where c.City.StartsWith("M")
   3:                     group c by c.City into g
   4:                     select new Result { City = g.Key, Count = g.Count() };

Untuk menampilkan hasilnya, kita bisa menggunakan perintah berikut:

   1: foreach (var r in results)
   2: {
   3:     Console.WriteLine("City:{0}\tCount:{1}", r.City, r.Count);
   4: } 
   5: Console.ReadKey();

dan keluarannya adalah seperti ini:

linq3

Telah kita lihat bagaiman cara membaca file xml, lalu bagaimana untuk membuat sebuah file xml?

berikut sedikit contoh:

   1: static void Main(string[] args)
   2:   {
   3:       Console.WriteLine("New Element"); 
   4:       var xml = new XElement("Car");
   5:       Console.WriteLine(xml);
   6:       Console.ReadKey();
   7:       Console.WriteLine("New Child Element"); 
   8:       var xml1 = new XElement("Car", new XElement("Engine", 1000));
   9:       Console.WriteLine(xml1);
  10:       Console.ReadKey();
  11:       Console.WriteLine("New Attribute of an Element"); 
  12:       var xml2 = new XElement("Car", new XAttribute("Engine", 1000));
  13:       Console.WriteLine(xml2);
  14:       Console.ReadKey();
  15:   }

Outputnya seperti berikut:

linq4

Ini bisa menjadi modal kita untuk menyusun sebuah file xml yang berasal dari query sebuah database yang dikombinasikan dengan query dari sebuah webservice atau sumber data lainnya.

Share this post: | | | |
Published Sunday, February 17, 2008 5:13 AM by tahir
Filed under:

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems