全体として、各エントリから「タイトル」と「cap:areaDesc」で構成される文字列を出力しようとしています。タイトルやその他の要素、さらには要素のリストを印刷できる場所にたどり着きました。彼らの「エントリー」のために一緒に出てくる必要があります。
私は、stackoverflow に関する数え切れないほどの投稿を確認してきました。そのうちのいくつかは、ここまで到達するのに役立ちましたが、この問題に対処するものは何も見つかりませんでした。最大の問題は、XML に対して適切な linq クエリを作成する際の問題だと思います。
私のロジックは、xmlstring を Xdocument または XElement (どちらが優れているかは不明) にロードし、「entry」という名前のすべての要素の IEnumerable リストを作成し、各エントリについてその子孫に移動し、両方の「title」の値を取得することです。 " element と cap:areaDesc" 要素。次に、文字列を 1 つの文字列に連結します。最後に、これらの文字列のそれぞれが、各エントリの title と areaDesc をリストする 1 つの長い文字列にまとめられます。テスト目的で、最終的なものを印刷しようとしています。コンソールの長い文字列。
追加する編集:「エントリ」タグの下にない開始近くに「タイトル」タグを印刷したくありません。「entry」タグの下には「title」と「cap:areaDesc」のみ。
XDocument root = XDocument.Parse(xmlString);
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom");
IEnumerable<XElement> xlist =
root.Descendants(ns + "entry").Select(elem => elem.Descendants(ns + "title").Single());
foreach (XElement el in xlist)
{
Console.WriteLine("title: " + el.Value);
}
これにより、各エントリのタイトルが出力されますが、タイトルと areaDesc を一緒に取得する方法がわかりません。
私も次のようなことを試しました:
XElement xdoc = XElement.Parse(xmlString);
IEnumerable<XElement> feed = xdoc.Elements();
Console.WriteLine("Current Alerts:");
foreach (var entry in feed)
{
Console.WriteLine("Alert: {0} has ID: {1}",
entry.Element("title").Value,
entry.Element("id").Value);
}
しかし、他の問題の中でも、名前空間を扱っていません。
文字列は次のとおりです (長くて申し訳ありませんが、このクラスの実行方法に影響するものは何も切りたくなかったので、2 つを除いてすべての「エントリ」タグを既に切り取りました)。
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>
<!--
This atom/xml feed is an index to active advisories, watches and warnings
issued by the National Weather Service. This index file is not the complete
Common Alerting Protocol (CAP) alert message. To obtain the complete CAP
alert, please follow the links for each entry in this index. Also note the
CAP message uses a style sheet to convey the information in a human readable
format. Please view the source of the CAP message to see the complete data
set. Not all information in the CAP message is contained in this index of
active alerts.
-->
<feed
xmlns = 'http://www.w3.org/2005/Atom'
xmlns:cap = 'urn:oasis:names:tc:emergency:cap:1.1'
xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
>
<!-- http-date = Thu, 19 Sep 2013 10:36:00 GMT -->
<id>http://alerts.weather.gov/cap/la.atom</id>
<logo>http://alerts.weather.gov/images/xml_logo.gif</logo>
<generator>NWS CAP Server</generator>
<updated>2013-09-19T05:36:00-05:00</updated>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Current Watches, Warnings and Advisories for Louisiana Issued by the National Weather Service</title>
<link href='http://alerts.weather.gov/cap/la.atom'/>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=LA124F1847F5BC.CoastalFloodAdvisory.124F1855EA00LA.LIXCFWLIX.9647349518939f7318139bd7d878c452</id>
<updated>2013-09-19T03:55:00-05:00</updated>
<published>2013-09-19T03:55:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Coastal Flood Advisory issued September 19 at 3:55AM CDT until September 19 at 7:00PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=LA124F1847F5BC.CoastalFloodAdvisory.124F1855EA00LA.LIXCFWLIX.9647349518939f7318139bd7d878c452"/>
<summary>...PERSISTENT EAST WINDS COMBINED WITH TIDES COULD CAUSE MINOR COASTAL FLOODING... .PROLONGED EAST WINDS HAVE RESULTED IN SLIGHT WATER BUILDUP WHICH MAY RESULT IN MINOR COASTAL FLOODING FOR EASTERN FACING SHORELINES THROUGH THIS EVENING. EXPECT TIDES TO BE ONE TO TWO FEET ABOVE NORMAL. IF LOW PRESSURE DEVELOPS OVER THE LOWER GULF OF MEXICO</summary>
<cap:event>Coastal Flood Advisory</cap:event>
<cap:effective>2013-09-19T03:55:00-05:00</cap:effective>
<cap:expires>2013-09-19T19:00:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Expected</cap:urgency>
<cap:severity>Minor</cap:severity>
<cap:certainty>Likely</cap:certainty>
<cap:areaDesc>LAZ069; Lower Jefferson; Lower Lafourche; Lower St. Bernard; Orleans; Upper St. Bernard</cap:areaDesc>
<cap:polygon></cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>022051 022057 022071 022087</value>
<valueName>UGC</valueName>
<value>LAZ062 LAZ064 LAZ067 LAZ068 LAZ069 LAZ070</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value>/O.CON.KLIX.CF.Y.0003.000000T0000Z-130920T0000Z/</value>
</cap:parameter>
</entry>
<entry>
<id>http://alerts.weather.gov/cap/wwacapget.php?x=LA124F183A324C.SpecialWeatherStatement.124F18497874LA.JANSPSJAN.39120e8b079d7137b7a148950e61a666</id>
<updated>2013-09-18T13:35:00-05:00</updated>
<published>2013-09-18T13:35:00-05:00</published>
<author>
<name>w-nws.webmaster@noaa.gov</name>
</author>
<title>Special Weather Statement issued September 18 at 1:35PM CDT by NWS</title>
<link href="http://alerts.weather.gov/cap/wwacapget.php?x=LA124F183A324C.SpecialWeatherStatement.124F18497874LA.JANSPSJAN.39120e8b079d7137b7a148950e61a666"/>
<summary>...HIGH FIRE DANGER CONDITIONS EXPECTED THROUGH THE END OF THE WEEK... ACCORDING TO AREA FORESTRY COMMISSIONS AND THE US FOREST SERVICE, RECENT DRY CONDITIONS HAVE MADE OUTDOOR BURNING INCREASINGLY HAZARDOUS. CONDITIONS ARE SUCH THAT A SPARK FROM EQUIPMENT, THE HEAT FROM A CATALYTIC CONVERTER, OR ANY HEAT SOURCE COULD START A GRASS</summary>
<cap:event>Special Weather Statement</cap:event>
<cap:effective>2013-09-18T13:35:00-05:00</cap:effective>
<cap:expires>2013-09-19T13:45:00-05:00</cap:expires>
<cap:status>Actual</cap:status>
<cap:msgType>Alert</cap:msgType>
<cap:category>Met</cap:category>
<cap:urgency>Expected</cap:urgency>
<cap:severity>Minor</cap:severity>
<cap:certainty>Observed</cap:certainty>
<cap:areaDesc>Catahoula; Concordia; East Carroll; Franklin; Madison; Morehouse; Richland; Tensas; West Carroll</cap:areaDesc>
<cap:polygon></cap:polygon>
<cap:geocode>
<valueName>FIPS6</valueName>
<value>022025 022029 022035 022041 022065 022067 022083 022107 022123</value>
<valueName>UGC</valueName>
<value>LAZ007 LAZ008 LAZ009 LAZ015 LAZ016 LAZ023 LAZ024 LAZ025 LAZ026</value>
</cap:geocode>
<cap:parameter>
<valueName>VTEC</valueName>
<value></value>
</cap:parameter>
</entry>
</feed>
助けや提案をありがとう。-TK