以下の変数と定数を定義
string choose_file; //選択したファイルの名前 int article_number; //文献数 string[] article_array; //PMIDの配列 const string ver_no = "1.0.0"; //バージョンナンバー
メニューバー「ヘルプ」/「バージョン情報」を定義
private void バージョン情報AToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("Version " + ver_no); }
メニューバー「ファイル」/「終了」を定義
private void 終了XToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); }
メニューバー「ファイル」/「開く」を定義
- まずは以下の「open_file.cs」と「make_html.cs」を追加
open_file.cs
using System; using System.Windows.Forms; namespace 文献管理 { public partial class Form1 { void open_file() { //OpenFileDialogクラスのインスタンスを作成 OpenFileDialog ofd = new OpenFileDialog(); //はじめに表示されるフォルダを指定する //指定しない(空の文字列)の時は、現在のディレクトリが表示される ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //「ファイルの種類」に表示される選択肢を指定する ofd.Filter = "XMLファイル(*.xml)|*.xml"; //タイトルを設定する ofd.Title = "開くファイルを選択してください"; //複数選択を不可にする ofd.Multiselect = false; //ダイアログを表示する if (ofd.ShowDialog() == DialogResult.OK) { choose_file = ofd.FileName; this.toolStripStatusLabel1.Text = choose_file; make_html(); } } } }
make_html.cs
using System.Linq; using System.Xml.Linq; using System.Xml.Xsl; namespace 文献管理 { public partial class Form1{ void make_html() { string cd = choose_file.Replace(".xml", "_temporary.html"); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("pub.xsl"); xslt.Transform(choose_file, cd); webBrowser1.Navigate(cd); XDocument xdoc = XDocument.Load(choose_file); article_number = xdoc.Descendants("PubmedArticle").Count(); if (article_number != 0) { article_array = new string[article_number]; var n = xdoc.Element("PubmedArticleSet").Elements("PubmedArticle").ToArray(); for (int i = 0; i < article_number; i++) { article_array[i] = n[i].Element("MedlineCitation").Element("PMID").Value; } } } } }
- 次に以下の「pub.xsl」を作成し実行ファイルと同じフォルダに保存する
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>文献リスト</title></head> <body> <table border="1" align="center"> <tr> <th width="8%" bgcolor="silver"> <font size="+2">PMID</font> </th> <th bgcolor="silver"> <font size="+2">title</font> </th> <th bgcolor="silver"> <font size="+2">journal</font> </th> </tr> <xsl:apply-templates select="//PubmedArticle"/> </table> </body> </html> </xsl:template> <xsl:template match="PubmedArticle"> <tr> <td> <font size="+2"> <a> <xsl:attribute name="href"><xsl:value-of select="concat('about:', MedlineCitation/PMID)" /></xsl:attribute> <xsl:value-of select="MedlineCitation/PMID"/> </a> </font> </td> <td> <font size="+2"> <xsl:value-of select="MedlineCitation/Article/ArticleTitle"/> </font> </td> <td> <font size="+2"> <xsl:value-of select="MedlineCitation/Article/Journal/Title"/> </font> </td> </tr> </xsl:template> </xsl:stylesheet>
- 最後に関数を呼び出すためのコードを追加
private void 開くOToolStripMenuItem_Click(object sender, EventArgs e) { open_file(); }
(補足)
スタイルシートをリソースとして埋め込むことも可能。
make_html.csの変更点
using System.Xml; using System.IO; string cd = choose_file.Replace(".xml", "_temporary.html"); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(new XmlTextReader(new StringReader(文献管理.Properties.Resources.pub))); xslt.Transform(choose_file, cd); webBrowser1.Navigate(cd);