かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[C#]LINQ to XMLでJOINしてみたよ

久しぶりにシンプルにVisual Studio 2008でコンソールアプリでお試し。
今日試すのは、LINQ to XMLだ!

仕事でどれくらい使うかは未知数というか0に限りなく近いけど、お試しをしてみる。

とりあえず、お試しようのプロジェクトを作成。名前はLINQtoXMLSampleにした。
もちろんコンソールアプリケーションで。

とりあえず、元になるXMLが必要になるけど、今回は練習ということでプログラム内にハードコーディングした。

        static readonly string EMPS = @"
<Emps>
  <Emp id='1'>
    <Name>田中 太郎</Name>
    <DeptId>1</DeptId>
  </Emp>
  <Emp id='2'>
    <Name>田中 次郎</Name>
    <DeptId>1</DeptId>
  </Emp>
  <Emp id='3'>
    <Name>田中 三郎</Name>
    <DeptId>2</DeptId>
  </Emp>
</Emps>
";
        static readonly string DEPTS = @"
<Depts>
  <Dept id='1'>
    <Name>人事部</Name>
  </Dept>
  <Dept id='2'>
    <Name>総務部</Name>
  </Dept>
</Depts>
";

従業員データをあらわすEmpsと部署データをあらわすDeptsというXMLになる。EmpsのDeptIdとDeptのid属性で紐づいてるっていう寸法だ。XMLなのに、こういうRDBみたいなデータの持たせ方するのはどうなの?という話もあるだろうけど、今回はJOINするのが目的なので、こういう形にしてみた。
ということで、このEmpsとDeptsというXMLを結合して、人の名前と、その人が所属してる部署名を抽出して抜き出してみようと思う。

LINQのjoinは、「join hoge in hogehoges on xxxxxx equals yyyyyy」みたいな雰囲気でかくっぽい。
ということでざっくり書いてみた。

// XMLをパース
var emps = XElement.Parse(EMPS);
var depts = XElement.Parse(DEPTS);

// EmpタグのDeptIdとDeptのid属性でjoinしてEmpのNameとDeptのNameを抽出
var results = from e in emps.Elements("Emp")
              join d in depts.Elements("Dept") on e.Element("DeptId").Value equals d.Attribute("id").Value
              select new { Name = e.Element("Name").Value, DeptName = d.Element("Name").Value };
// 結果を表示
foreach (var result in results)
{
    Console.WriteLine(result);
}

これを実行すると、下のような結果になる。

{ Name = 田中 太郎, DeptName = 人事部 }
{ Name = 田中 次郎, DeptName = 人事部 }
{ Name = 田中 三郎, DeptName = 総務部 }

うまく、いった。いい感じだ?

投稿日時 : 2008年6月24日 23:43

Feedback

# re: [C#]LINQ to XMLでJOINしてみたよ 2008/06/25 0:00 かずき

ついでに感想は、LINQ to SQLやLINQ to ObjectsよりLINQ to XMLを書くのは疲れた。
きっとCtrl + Spaceで色々便利に保管してくれないからだろう…

抽出条件によっては、XPathのほうが楽チンかもしれない。
状況によって使い分けよう.

# supra uk 2012/12/08 11:31 http://supratkstore.webs.com/

Appreciate it for helping out, fantastic information. "Hope is the denial of reality." by Margaret Weis.

# sac longchamp pliage 2012/12/14 20:45 http://www.soldesacslongchamp.info/category/longch

I truly like em. It's unlike you wear these out walking on town men and women.

# トリーバーチ 財布 2012/12/16 17:59 http://www.torybruchjp.info

I haven't looked right into Sennheisers and additionally am short of new tote.

# michael kors sac solde 2012/12/18 2:14 http://michael-kors-canada.webnode.fr/news-/

Think it like a journal or maybe a magazine associated with general interest for those who to read through. Funny, witty, controversial, entertaining, useful, acerbic, thoughtful, serious, curious, unexpected comebacks can be welcome.

# Everything information about medication. What side effects can this medication cause?
https://edonlinefast.com
drug information and news for professionals and consumers. Read now. 2023/02/18 0:40 EdOnline

Everything information about medication. What side effects can this medication cause?
https://edonlinefast.com
drug information and news for professionals and consumers. Read now.

# pain medications without a prescription - https://cheapdr.top/# 2023/04/03 15:15 Dikolipo

pain medications without a prescription - https://cheapdr.top/#

# prednisone 40 mg tablet - https://prednisonesale.pro/# 2023/04/22 15:04 Prednisone

prednisone 40 mg tablet - https://prednisonesale.pro/#

# Cytotec 200mcg price - https://cytotecsale.pro/# 2023/04/29 4:34 Cytotec

Cytotec 200mcg price - https://cytotecsale.pro/#

# wellcare over the counter ordering https://overthecounter.pro/# 2023/05/08 22:38 OtcJikoliuj

wellcare over the counter ordering https://overthecounter.pro/#

# best treatment for ed https://edpill.pro/# - medications for ed 2023/06/27 14:32 EdPills

best treatment for ed https://edpill.pro/# - medications for ed

# paxlovid price https://paxlovid.store/
buy paxlovid online 2023/07/13 21:38 Paxlovid

paxlovid price https://paxlovid.store/
buy paxlovid online

# cytotec pills buy online https://cytotec.ink/# - purchase cytotec 2023/07/27 1:07 PillsFree

cytotec pills buy online https://cytotec.ink/# - purchase cytotec

# doxycycline 100mg capsules https://doxycycline.forum/ price of doxycycline 2023/11/25 13:09 Doxycycline

doxycycline 100mg capsules https://doxycycline.forum/ price of doxycycline

# comprare farmaci online con ricetta https://farmaciait.pro/ farmacia online miglior prezzo 2023/12/04 10:09 Farmacia

comprare farmaci online con ricetta https://farmaciait.pro/ farmacia online miglior prezzo

# buy ed pills online https://edpills.tech/# ed treatment review 2023/12/23 8:06 EdPills

buy ed pills online https://edpills.tech/# ed treatment review

タイトル
名前
Url
コメント