MSDN Documentation

System.Xml.XmlNodeReader.ReadGuid()

Represents a reader that provides fast, forward-only access to a node set that has been extracted from an XmlDocument.

Overload List

The following overloads are available for this method:

Syntax

C#
public override bool ReadGuid();

Parameters

There are no parameters for this method.

Return Value

true if the current node is a Guid; otherwise, false.

Remarks

The ReadGuid method provides a fast way to read the current node as a Guid. If the current node cannot be represented as a Guid, this method returns false.

This method is part of the XmlNodeReader class, which allows for efficient traversal of XML data originating from an XmlDocument. It avoids the overhead of parsing the entire XML document again.

Example

C#

using System;
using System.Xml;

public class XmlNodeReaderExample
{
    public static void Main(string[] args)
    {
        string xmlString = "<root><myGuid>{00000000-0000-0000-0000-000000000000}</myGuid></root>";
        
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlString);

        XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);

        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element && reader.Name == "myGuid")
            {
                // Move to the text node within the element
                reader.Read(); 
                
                if (reader.IsLocalName("myGuid")) // Check if it's the correct element name
                {
                    Guid guidValue;
                    if (Guid.TryParse(reader.Value, out guidValue))
                    {
                        Console.WriteLine($"Successfully read Guid: {guidValue}");
                    }
                    else
                    {
                        Console.WriteLine("Failed to parse Guid.");
                    }
                }
            }
        }
        reader.Close();
    }
}
                

See Also