Search code examples
c#xpathxmlnodelist

C# XPath / formatting decimal places of variable type XmlNodeList


I am looking to format my year.InnerXml to 3 decimal places. I have tried things like String.Format but that doesn't seem to work. any ideas?

  XmlNodeList getYears = doc.SelectNodes($"/ghg-canada/region[{choice}]/source/emissions[@year >= {yearsEntered[0]} and @year <= {yearsEntered[1]}]");

  foreach (XmlNode year in getYears)
  {   
      string formatted = String.Format("{0:#,0.000}", year.InnerXml);
      Console.Write("\t" + formatted);
  }

one of the 'emission' elements looks something like: 16.55535068.

I am hoping to truncate it to only 3 digits after the decimal point.

currently, the emissions do not get truncated at all.


Solution

  • Use year.CreateNavigator().ValueAsDouble instead of year.InnerXml.