0

I've come across different ways of getting the last element (there are several <hour> elements and two other but only the last <hour> element is the target needed) on the following link, e.g.:

XDocument xDoc = XDocument.Load("Source.xml");          
XElement lastHour = doc.Root.Elements("hour").Last();

It doesn't work for me. When I put down 'dot' after Elements("hour"), trying to call Last() method, SharpDevelop doesn't see it. What's wrong with it?

The whole block of code is:

if (hour == hisDoc.Root.Elements("hour").Last) {
     if ((hisDoc.Element("scanerHIS").Element("constants") != null) && (fcsDay.XPathSelectElement("constants") == null)) {
     fcsDay.Add(new XElement("constants",
          new XAttribute("P001", hisDoc.Root.XPathSelectElement("constants").Attribute("P001").Value),
          new XAttribute("Q001", hisDoc.Root.XPathSelectElement("constants").Attribute("Q001").Value),
          new XAttribute("N2", hisDoc.Root.XPathSelectElement("constants").Attribute("N2").Value),
          new XAttribute("CO2", hisDoc.Root.XPathSelectElement("constants").Attribute("CO2").Value)));
     }
}
fcsDoc.Save(FCSfileName); 

1 Answers1

0

Firstly, thanks to everyone who helped.

I need to write using System.Linq, because System.Xml.Linq doesn't contain Last() method.

The answer is:

if (hour == hisDoc.Root.Descendants("hour").Last())