XmlNodeReader.ReadDouble()

public double ReadDouble()

Reads the current node's value as a double.

Remarks

This method reads the attribute value, element value, or text content of the current node and returns it as a double. If the value cannot be parsed as a double, an exception is thrown. This method is useful when you need to retrieve numerical data from XML in a strongly typed format.

Note: Ensure that the value of the current node can be represented as a double. If not, a FormatException will be thrown.

Example

// Assume xmlString contains XML like:
// <root><value type="double">123.45</value></root>

using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        string xmlString = "123.45";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlString);

        XmlNodeReader reader = new XmlNodeReader(doc);

        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element && reader.Name == "value")
            {
                if (reader.HasAttributes && reader.GetAttribute("type") == "double")
                {
                    reader.Read(); // Move to the text content node
                    try
                    {
                        double doubleValue = reader.ReadDouble();
                        Console.WriteLine($"Read double value: {doubleValue}");
                    }
                    catch (FormatException ex)
                    {
                        Console.WriteLine($"Error parsing double: {ex.Message}");
                    }
                }
            }
        }
    }
}

See Also