ぽざうねっと

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  420  : 記事  0  : コメント  9422  : トラックバック  76

ニュース

さて、今年は何を漬けようか

書庫

日記カテゴリ

Accessory

Community

Private

さて LINQ の式にスポットを当ててみましょう。
Elements メソッドは子要素の IEnumerable<XElement> を取得します。
引数に XName を渡すと、指定のの名前の要素のみ列挙します。
もっと複雑な取得方法もできます。
たとえばこんな XML に対して同じことをしてみましょう。
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <child>
        <item key="11">1-1</item>
        <item key="12">1-2</item>
        <item key="13">1-3</item>
    </child>
    <child>
        <item key="21">2-1</item>
        <item key="22">2-2</item>
        <item key="23">2-3</item>
    </child>
    <child>
        <item key="31">3-1</item>
        <item key="32">3-2</item>
        <item key="33">3-3</item>
    </child>
</root>
Elements メソッドを使うとこうなります。
IEnumerable<XElement> elements =
    from child in documentElement.Elements()
    from item in documentElement.Elements()
    where item.Attribute("key").Value.EndsWith("1")
    select item;
ところが DescendantsAndSelf というメソッドがありまして、これを使うと下記の様にできます。
IEnumerable<XElement> elements =
    from item in documentElement.DescendantsAndSelf("item")
    where item.Attribute("key").Value.EndsWith("1")
    select item;
ピンと来た方もいらっしゃるでしょう。
XPath には軸という概念があります。
で、この軸ごとにメソッドがあるみたいです。
ざっと列挙すると
メソッド
Elementschild
Attributesattribute
Ancestorsancestor
AncestorsAndSelfancestor-or-self
Descendantsdescendant
DescendantsAndSelfdescendant-or-self
NodesAfterSelffollowing-sibiling
NodesBeforeSelfpreceding-sibiling
こんな感じかなぁ、全部の軸はなさげですね。(following とかは 親を取得してやれってことかな?)
軸名とメソッド名がリンクしていないのが惜しいです・・・・
投稿日時 : 2007年4月22日 10:41

コメント

No comments posted yet.

Post Feedback

タイトル
名前
Url:
コメント: