简体   繁体   中英

C# - how to get a specific value from a xml

I know this question has been asked a lot but I couldn't find one that suited me. I'm trying to get the weather from BBC Weather using their RSS feed. In this RSS feed, I'am trying to get the value of the second title (**).

Rss Feed:

<?xml version="1.0" encoding="UTF-8"?>
<rss>
  <channel>
    <title>BBC Weather - Observations for  Hampstead, GB</title>
    <link>https://www.bbc.co.uk/weather/2647553</link>
    <description>Latest observations for Hampstead from BBC Weather, </description>
    <language>en</language>
    <copyright>Copyright: (C) British Broadcasting Corporation,</copyright>
    <pubDate>Wed, 30 Oct 2019 14:00:00 GMT</pubDate>
    <dc:date>2019-10-30T14:00:00Z</dc:date>
    <dc:language>en</dc:language>
    <dc:rights>Copyright: (C) British Broadcasting Corporation, </dc:rights>
    <atom:link rel="self" />
    <item>
      **<title>Wednesday - 14:00 GMT: Not available, 11°C (52°F)</title>**
      <link>https://www.bbc.co.uk/weather/2647553</link>
      <description>Temperature: 11°C (52°F), Wind Direction: East South Easterly, Wind Speed: 13mph, Humidity: 58%, Pressure: 1023mb, Falling, Visibility: Excellent</description>
      <pubDate>Wed, 30 Oct 2019 14:00:00 GMT</pubDate>
      <guid isPermaLink="false">https://www.bbc.co.uk/weather/2647553-2019-10-30T14:00:00.000Z</guid>
      <dc:date>2019-10-30T14:00:00Z</dc:date>
      <georss:point>51.5574 -0.1821</georss:point>
    </item>
  </channel>
</rss>

I know it is wrong but what I got until now was this:

XDocument X = XDocument.Load("https://weather-broker-cdn.api.bbci.co.uk/en/observation/rss/2647553");
var weather = X.Element("channel").Element("item").Element("title").Value;
weather = Regex.Match(weather, @"GMT: (.+?),").Groups[1].Value;
XmlLabel.Text = weather;

I really can't find anywhere where it explains it clearly, Thanks in advance

Your code starts one level too deep:

var weather = X.Element("channel").Element("item").Element("title").Value;

You need to start from the root Element, which is "rss". So this will work:

var weather = X.Element("rss").Element("channel").Element("item").Element("title").Value;

But I'd recommend the shorter XPath variant:

X.XPathSelectElement("//item/title").Value;

I verified both variants above and your regex in a Fiddle with following code:

XDocument X = XDocument.Load("https://weather-broker-cdn.api.bbci.co.uk/en/observation/rss/2647553");

var test = X.XPathSelectElement("//item/title").Value;
Console.WriteLine(Regex.Match(test, @"GMT: (.+?),").Groups[1].Value);

var weather = X.Element("rss").Element("channel").Element("item").Element("title").Value;
Console.WriteLine(Regex.Match(weather, @"GMT: (.+?),").Groups[1].Value);

Output:

    Not available
    Not available

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM