XmlReader reader = XmlReader.Create("example.xml"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "price") { float price = reader.ReadElementContentAsFloat(); Console.WriteLine("Price: {0}", price); } }
using System.Xml.Linq; XElement root = XElement.Parse("In this example, an XElement is created from a string of XML. The Descendants method is used to find the element with the name "price", and the float value is obtained by casting it to a float type. The System.Xml namespace provides the XmlReader class while the System.Xml.Linq namespace provides the XElement class used in the second example."); float price = (float)root.Descendants("price").FirstOrDefault(); Console.WriteLine("Price: {0}", price); T1 12.99