良い一日。最初に警告しておきますが、XML に関する私の知識は基本的なものなので、研究で正しい答えを見つけたとしても、それを適用する方法を理解していないだけかもしれません。
私は現在、National Weather Service XML ファイルを読み取り、要素内のテキストを検索して気象警報があるかどうかを判断する C# コードをいくつか持っています。それは機能しますが、別の要素の存在をテストしてから、そのテキストを使用してアラートを Web ページに書き出すほうがよいでしょう。警告のある XML ファイルの例は、http: //www.co.frederick.va.us/dev/scrapewarning.xmlです。<cap:event>
の存在をテストし、そのテキストを使用して Web ページのリテラルを埋めたいと考えています。
これが私が今していることです:
// Create an instance of XmlReader with the warning feed and then load it into a SyndicationFeed.
XmlReader reader = XmlReader.Create(strWarningFeed);
SyndicationFeed feed = SyndicationFeed.Load(reader);
// Read through the XML, pull out the items we need depending on whether or not there is a warning.
foreach (var str in feed.Items)
{
if (str.Title.Text.Contains("no active"))
{
weatherWarning.Visible = false;
}
else
{
string strTitle = str.Title.Text;
string strId = str.Id.ToString();
strTitle = strTitle.Substring(0, strTitle.LastIndexOf("issued"));
litOut.Text += String.Format("<p class=\"warningText\">{0}</p><p class=\"warningText\"><a href=\"{1}\">Read more.</a></p>", strTitle, strId);
}
}
したがって、タイトルに「no active」が含まれているのを見る代わりに、ドキュメントに要素が含まれているかどうかを確認し、<cap:event>
そのテキストを使用したいと思います。疑似コード:
foreach (var str in feed.Items)
{
if (<cap:event> doesn't exist)
{
weatherWarning.Visible = false;
}
else
{
string strTitle = <cap:event>.Text;
}
}
さらに情報が必要な場合はお知らせください。助けてくれてありがとう。