总的来说,我试图从每个条目中打印一个由“title”和“cap:areaDesc”组成的字符串。我已经到了可以打印标题或其他元素,甚至是元素列表的位置。我需要他们一起出来“进入”。
我在 stackoverflow 上浏览过无数帖子,其中一些帮助我走到了这一步,但我还没有找到任何可以解决这个问题的方法。我认为最大的问题是我很难对 XML 形成良好的 linq 查询。
我的逻辑是将 xmlstring 加载到 Xdocument 或 XElement 中(不确定哪个更好),创建一个名为“entry”的所有元素的 IEnumerable 列表,然后对于每个条目,进入其后代并获取两个“title”的值“ element 和 cap:areaDesc” 元素。然后将字符串连接成一个字符串。最后,这些字符串中的每一个将组合成一个长字符串,其中列出每个条目的标题和 areaDesc。出于测试目的,我正在尝试打印最终结果控制台中的长字符串。
编辑添加:我不想在开头附近打印不在任何“条目”标签下的“标题”标签;“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);
}
但它不处理命名空间等问题。
这是字符串(对不起,长度,但我不想剪掉任何影响这个类将如何运行的东西,我已经剪掉了除两个之外的所有“条目”标签)。
<?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