XMLとLINQで扱う
LINQは様々なデータに対応しています。データを保存形式であるXMLのデータも取り出すことができます。
コードを作成してみましょう。このコードを実行するためには、Cドライブの下にsample.xmlが保存されているかを確認してください。また、このプログラムをビルドするためには、「System.Xml」と「System.Xml.Linq」への参照の追加が必要となります。
//sample4.cs
using System;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
namespace lesson11
{
class sample4 : Form
{
private ListBox lbx;
static void Main(string[] args)
{
Application.Run(new sample4());
}
public sample4()
{
this.Text = "lesson11 XMLとLINQ"; this.Width = 300; this.Height = 200;
lbx = new ListBox(); lbx.Dock = DockStyle.Fill;
//XMLを読み込みます
XDocument doc = XDocument.Load("E:\\099_Technology\\C#\\csharp\\csharp_train\\lesson9\\sample.xml");
IEnumerable qry = from K in doc.Descendants("car") select K;
foreach (var tmp in qry)
{
lbx.Items.Add(tmp);
}
lbx.Parent = this;
}
}
}
XMLをLINQで扱うためには、XDocumentクラスのLoad()メソッドで読み込みます。XDocumentクラスのDescendants()メソッドを指定した要素のコレクションを取得できます。この要素を、さきほどの配列と同様に扱うことができます。
このように、XMLでも配列でも、LINQにしたがうことによって同じ構文で問合わせを行うことができます。
条件をつけて検索する
LINQを使ってさまざまな検索を行うことができます。まずcountry属性の値が「日本」であるcar要素を検索してみましょう。
//sample5.cs
using System;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
namespace lesson11
{
class sample5 : Form
{
private ListBox lbx;
static void Main(string[] args)
{
Application.Run(new sample5());
}
public sample5()
{
this.Text = "lesson11 XMLとLINQ"; this.Width = 300; this.Height = 200;
lbx = new ListBox(); lbx.Dock = DockStyle.Fill;
//XMLを読み込みます
XDocument doc = XDocument.Load("E:\\099_Technology\\C#\\csharp\\csharp_train\\lesson9\\sample.xml");
IEnumerable qry = from K in doc.Descendants("car") where (string) K.Attribute("country") == "日本" select K;
foreach (var tmp in qry)
{
lbx.Items.Add(tmp);
}
lbx.Parent = this;
}
}
}
XElementクラスのAttribute()メソッドを使うと、この要素以下の指定した属性名の属性を得ることができます。この値を検索条件に使って調べているのです。
クラス | 説明 |
---|---|
System.Xml.Linq.XElementクラス | |
XAttribute Attribute(XName name)メソッド | 指定名の属性を取得する |
要素の値を取り出す
次に、id属性が1005以上のcar要素を検索してみましょう。今度はcar要素の下のname要素の値である、車の名前だけを表示してみます。
//sample5.cs
using System;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
namespace lesson11
{
class sample6 : Form
{
private ListBox lbx;
static void Main(string[] args)
{
Application.Run(new sample6());
}
public sample6()
{
this.Text = "lesson11 XMLとLINQ"; this.Width = 300; this.Height = 200;
lbx = new ListBox(); lbx.Dock = DockStyle.Fill;
//XMLを読み込みます
XDocument doc = XDocument.Load("E:\\099_Technology\\C#\\csharp\\csharp_train\\lesson9\\sample.xml");
IEnumerable qry = from K in doc.Descendants("car") where (int)K.Attribute("id") >= 1005 select K.Element("name").Value;
foreach (var tmp in qry)
{
lbx.Items.Add(tmp);
}
lbx.Parent = this;
}
}
}
XElementクラスのElement()メソッドを使うと、この要素以下の指定した要素名の要素を得ることができます。ここでは要素の値を取り出すことにしています。
クラス | 説明 |
---|---|
System.Xml.Linq.XElementクラス | |
XAttribute Attribute(XName name)メソッド | 指定名の最初の子要素を取得する |
Valueプロパティ | 要素の値を設定・取得する |
並べ替えを行う
並べ替えを行うこともできます。配列のときと同様にorderbyを指定して並べ替えてみましょう。
//sample5.cs
using System;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
namespace lesson11
{
class sample7 : Form
{
private ListBox lbx;
static void Main(string[] args)
{
Application.Run(new sample7());
}
public sample7()
{
this.Text = "lesson11 XMLとLINQ"; this.Width = 300; this.Height = 200;
lbx = new ListBox(); lbx.Dock = DockStyle.Fill;
//XMLを読み込みます
XDocument doc = XDocument.Load("E:\\099_Technology\\C#\\csharp\\csharp_train\\lesson9\\sample.xml");
IEnumerable qry = from K in doc.Descendants("car") orderby (int) K.Attribute("id") select K.Element("name").Value;
foreach (var tmp in qry)
{
lbx.Items.Add(tmp);
}
lbx.Parent = this;
}
}
}
id属性の値の順に並べ替えたうえで、要素の値を取り出すことができます。
このように、LINQによってXMLからさまざまな取り出しができるようになります。便利な方法として覚えておくとよいでしょう。
コメント