Silverlight では、Language-Integrated Query (LINQ) to XML または XmlReader クラスを使用して、XML データを解析できます。XML の一般的な利用法としては、RSS フィードを解析および変換して、コンテンツをサイトに表示するといったケースが挙げられます。RSS は XML 形式であるため、LINQ to XML を使用すれば RSS フィードのクエリと変換を簡単に実行できます。
通常、RSS フィードにはニュース コンテンツが格納されますが、個別の項目に分類可能なあらゆるコンテンツを含めることができます。最も単純な形の RSS フィードは、タイトル、リンク、説明を持つチャネルで構成されます。また、RSS フィードには、タイトル、リンク、説明などの要素を持つ 1 つまたは複数の項目要素を格納することができます。
このクイックスタートでは、プログラミング関連書籍の RSS フィードを解析する方法について説明します。特に、RSS フィードの読み込み、指定要素による項目のフィルター処理、項目のグループ化や並べ替え、XML ツリーの構造の変更、および射影の型の制御について説明します。
このクイックスタートは、次のセクションで構成されています。
LINQ to XML の詳細については、MSDN の Silverlight ドキュメントの「LINQ to XML による XML データの処理 (Silverlight) 」を参照してください。
RSS ファイルの読み込み
Silverlight では、次の方法で XML ファイルを読み込むことができます。
次の例は、アプリケーションの XAP パッケージから RSS ファイルの読み込みと表示を行う方法を示します。
C#
XElement rss = XElement.Load("rss.xml"); OutputTextBoxLoadRss.Text = rss.ToString();
Visual Basic
Dim rss As XElement = XElement.Load("rss.xml") OutputTextBoxLoadRss.Text = rss.ToString()
次に、RSS ファイルのコンテンツを示します。
<rss version="2.0"> <channel> <title>Programming Books</title> <link>http://www.books.microsoft.com</link> <description>Download programming books</description> <pubDate>Thu, 27 Apr 2008 13:40:10 +0000</pubDate> <language>en</language> <item> <title> C# programming for beginners </title> <link>http://www.books.microsoft.com/beginners-cs/</link> <pubDate>Thu, 17 Apr 2008 13:40:10 +0000</pubDate> <category>C Sharp</category> <description> This is C# for beginners. </description> </item> <item> <title> VB programming for beginners </title> <link>http://www.books.microsoft.com/beginners-vb/</link> <pubDate>Thu, 15 Apr 2006 13:40:10 +0000</pubDate> <category>Visual Basic</category> <description> This is VB for beginners. </description> </item> <item> <title> C# in Depth </title> <link>http://www.books.microsoft.com/advanced-cs/</link> <pubDate>Thu, 13 Dec 2007 13:40:10 +0000</pubDate> <category>C Sharp</category> <description> This is advanced C# programming book. </description> </item> <item> <title> VB in Depth </title> <link>http://www.books.microsoft.com/advanced-vb/</link> <pubDate>Thu, 17 Oct 2008 13:40:10 +0000</pubDate> <category>Visual Basic</category> <description> This is advanced VB programming book. </description> </item> </channel> </rss>
特定の要素による RSS XML ツリーのフィルター処理
XML ツリー全体ではなく、XML データのサブセットを操作したい場合がよくあります。LINQ to XML では、クエリと機能構造を組み合わせて、元のドキュメントとは異なる構造を持つ新しい XML ドキュメントを生成できます。特定の要素で XML ドキュメントをフィルター処理して、目的のデータを抽出できます。
次の例では、値が "C#" の category 子要素を持つ item 要素を検索するクエリを作成しています。このクエリは、XElement の IEnumerable を返します。クエリの結果は、XElement のコンストラクターの第 2 パラメーターとして渡されます。最後に、このコードは新しく作成された XElement オブジェクトのコンテンツをページに出力します。
C#
XElement filterCategory = new XElement("FilteringByCategory", from category in rss.Element("channel").Elements("item") where (string)category.Element("category") == "C Sharp" select new XElement("item", category.Elements() ) ); OutputTextBoxFilter.Text = filterCategory.ToString();
Visual Basic
Dim filterCategory = <FilteringByCategory> <%= From category In rss.<channel>.<item> _ Where category.<category>.Value.Contains("C Sharp") _ Select _ <item> <%= category.Elements %> </item> _ %></FilteringByCategory>
OutputTextBoxFilter.Text = filterCategory.ToString()
次に、"C#" カテゴリによるフィルター処理のクエリ出力を示します。
<FilteringByCategory> <item> <title> C# programming for beginners </title> <link>http://www.books.microsoft.com/beginners-cs/</link> <pubDate>Thu, 17 Apr 2008 13:40:10 +0000</pubDate> <category>C Sharp</category> <description> This is C# for beginners. </description> </item> <item> <title> C# in Depth </title> <link>http://www.books.microsoft.com/advanced-cs/</link> <pubDate>Thu, 13 Dec 2007 13:40:10 +0000</pubDate> <category>C Sharp</category> <description> This is advanced C# programming book. </description> </item> </FilteringByCategory>
カテゴリによる RSS XML ツリーのグループ化
データをグループに分類すると読み込みや分析が簡単になることが数多く報告されています。
次の例は、データをグループ化し、このグループ化に基づいて XML を生成する方法を示します。この例では、item 要素をカテゴリごとにグループ化するクエリを作成します。このクエリは、XElement の IEnumerable を返します。クエリの結果は、XElement のコンストラクターの第 2 パラメーターとして渡されます。最後に、このコードは新しく作成された XElement オブジェクトのコンテンツをページに出力します。
C#
XElement groupCategory = new XElement("GroupingByCategory", from item in rss.Element("channel").Elements("item") group item by (string)item.Element("category") into groupedData select new XElement("group", new XAttribute("Name", groupedData.Key), from g in groupedData select new XElement("item", g.Elements()) ) );
OutputTextBoxGroup.Text = groupCategory.ToString();
Visual Basic
Dim groupCategory = <GroupingByCategory> <%= From item In rss.<channel>.<item> _ Group By item = item.<category>.Value _ Into groupedData = Group _ Select _ <group Name=<%= item %>> <%= From g In groupedData _ Select _ <item> <%= g.Elements %> </item> %> </group> %> </GroupingByCategory>
OutputTextBoxGroup.Text = groupCategory.ToString()
次に、カテゴリごとのグループ化のクエリ出力を示します。
<GroupingByCategory> <group Name="C Sharp"> <item> <title> C# programming for beginners </title> <link>http://www.books.microsoft.com/beginners-cs/</link> <pubDate>Thu, 17 Apr 2008 13:40:10 +0000</pubDate> <category>C Sharp</category> <description> This is C# for beginners. </description> </item> <item> <title> C# in Depth </title> <link>http://www.books.microsoft.com/advanced-cs/</link> <pubDate>Thu, 13 Dec 2007 13:40:10 +0000</pubDate> <category>C Sharp</category> <description> This is advanced C# programming book. </description> </item> </group> <group Name="Visual Basic"> <item> <title> VB programming for beginners </title> <link>http://www.books.microsoft.com/beginners-vb/</link> <pubDate>Thu, 15 Apr 2006 13:40:10 +0000</pubDate> <category>Visual Basic</category> <description> This is VB for beginners. </description> </item> <item> <title> VB in Depth </title> <link>http://www.books.microsoft.com/advanced-vb/</link> <pubDate>Thu, 17 Oct 2008 13:40:10 +0000</pubDate> <category>Visual Basic</category> <description> This is advanced VB programming book. </description> </item> </group> </GroupingByCategory>
タイトルによる RSS XML ツリーの並べ替え
XML ツリーから特定の要素を抽出し、それらを並べ替えて表示することが必要な場合があります。
次の例は、結果を並べ替えるクエリの作成方法を示します。この例では、title ごとに item 要素を並べ替えるクエリを作成します。このクエリは、String の IEnumerable を返します。
C#
IEnumerable<String> sortedTitles = from item in rss.Elements("channel").Elements("item") let title = (String)item.Element("title") orderby title select title;
StringBuilder sb = new StringBuilder(); sb.AppendLine("Sorting by Title:");
foreach (String bookTitle in sortedTitles) sb.AppendLine(bookTitle.ToString());
OutputTextBoxSort.Text = sb.ToString();
Visual Basic
Dim sortedTitles As IEnumerable(Of String) = _ From item In rss.<channel>.<item> _ Let title = Convert.ToString(item.<title>.Value) _ Order By (title) _ Select title
Dim sb As New StringBuilder sb.AppendLine("Sorting by Title:") Dim bookTitle As String For Each bookTitle In sortedTitles sb.AppendLine(bookTitle.ToString()) Next bookTitle OutputTextBoxSort.Text = sb.ToString()
次に、タイトルごとの並べ替えのクエリ出力を示します。
Sorting by Title: C# in Depth C# programming for beginners VB in Depth VB programming for beginners
RSS XML ツリーの構造の変換
XML ドキュメントの構造とは、要素名、属性名、および階層の特性を指します。XML ドキュメントの構造の変更が必要になる場合があります。たとえば、既存の XML ドキュメントを別のシステムに送信するにあたって、要素名と属性名の変更が必要となる場合があります。
次の例では、埋め込みのクエリ式を使用して XML ファイルの構造を変更します。この例のソース XML ドキュメントでは、rss ルート要素の下に channel 要素があります。channel 要素には、タイトル、リンク、説明、および項目要素が含まれており、その項目要素のそれぞれにタイトル、リンク、説明、および他の要素が含まれています。変換後の XML ツリーでは、TransformingShape 要素の下に category、title、および pubDate の各子要素を持つ book 要素が作成されます。
C#
XElement transformShape = new XElement("TransformingShape", from item in rss.Elements("channel").Elements("item") select new XElement("book", item.Elements("category"), item.Elements("title"), item.Elements("pubDate")) );
OutputTextBoxTransform.Text = transformShape.ToString();
Visual Basic
Dim transformShape = <TransformingShape> <%= From item In rss.<channel>.<item> _ Select <book> <%= item.<category> %> <%= item.<title> %> <%= item.<pubDate> %> </book> %> </TransformingShape>
OutputTextBoxTransform.Text = transformShape.ToString()
次に、構造変換のクエリ出力を示します。
<TransformingShape> <book> <category>C Sharp</category> <title> C# programming for beginners </title> <pubDate>Thu, 17 Apr 2008 13:40:10 +0000</pubDate> </book> <book> <category>Visual Basic</category> <title> VB programming for beginners </title> <pubDate>Thu, 15 Apr 2006 13:40:10 +0000</pubDate> </book> <book> <category>C Sharp</category> <title> C# in Depth </title> <pubDate>Thu, 13 Dec 2007 13:40:10 +0000</pubDate> </book> <book> <category>Visual Basic</category> <title> VB in Depth </title> <pubDate>Thu, 17 Oct 2008 13:40:10 +0000</pubDate> </book> </TransformingShape>
射影の型の制御
射影は、1 つのデータ セットを取得して、フィルター処理し、その形式を変更し、その型も変更するプロセスです。ほとんどのクエリ式は射影を実行します。このトピックのクエリ式は、ほとんどが XElement の IEnumerable に評価されますが、射影の型を制御することで別の型のコレクションを構築できます。
次の例では、Book という新しい型を定義します。次に、クエリ式の Select 句で新しい Book オブジェクトをインスタンス化します。これにより、クエリ式の型が Book の IEnumerable になります。
C#
public partial class Page : UserControl { public class Book { private string category; private string title;
public Book(string category, string title) { this.category = category; this.title = title; }
public override string ToString() { return String.Format("Category: {0} " + "Title:{1}", this.category, this.title); } }
void ControllingProjectionType(XElement rss) { IEnumerable<Book> bookList = from item in rss.Elements("channel").Elements("item") select new Book( (string)item.Element("category"), (string)item.Element("title") ); StringBuilder sb = new StringBuilder(); sb.AppendLine("Controlling Projection Type:"); foreach (Book book in bookList) sb.AppendLine(book.ToString());
OutputTextBoxControlType.Text = sb.ToString(); }
public Page() { InitializeComponent(); // Load the rss file. XElement rss = XElement.Load("rss.xml"); ControllingProjectionType(rss); } }
Visual Basic
Partial Public Class Page Inherits UserControl
Public Class Book Private category As String Private title As String
Public Sub New(ByVal category As String, _ ByVal title As String) Me.category = category Me.title = title End Sub
Public Overrides Function ToString() As String Return String.Format("Category: {0} " & _ "Title:{1}", Me.category, Me.title) End Function End Class
Sub ControllingProjectionType(ByVal rss As XElement) Dim bookList As IEnumerable(Of Book) = _ From item In rss.<channel>.<item> _ Select New Book( _ item.<category>.Value, _ item.<title>.Value)
Dim sb As New StringBuilder() sb.AppendLine("ControllingType:") Dim book As Book For Each book In bookList sb.AppendLine(book.ToString()) Next book OutputTextBoxControlType.Text = sb.ToString() End Sub 'ControlProjectionType
Public Sub New() InitializeComponent() Dim rss As XElement = XElement.Load("rss.xml") ControllingProjectionType(rss) End Sub End Class
次に、Book 型への射影の出力を示します。
Controlling Projection Type: Category: C Sharp Title: C# programming for beginners Category: Visual Basic Title: VB programming for beginners Category: C Sharp Title: C# in Depth Category: Visual Basic Title: VB in Depth
参照
フィードバックを送信する