XmlSelectionRouteNodeValueCollection

Namespace: System.Xml

Overview

Represents a collection of XmlAttribute, XmlCdataSection, XmlComment, XmlDeclaration, XmlDocumentType, XmlEntity, XmlEntityReference, XmlNotation, XmlProcessingInstruction, or XmlText objects.

The XmlSelectionRouteNodeValueCollection class is used to store a collection of XML node values that can be represented by specific node types within the .NET Framework's XML object model. This collection is typically encountered when querying XML documents using XPath or other selection mechanisms, where the results might be a set of these primitive or structural XML node types.

It provides methods and properties for accessing, iterating, and managing the individual node values within the collection.

Members

Properties

Name Description
Count Gets the number of elements contained in the XmlSelectionRouteNodeValueCollection.
Item(Int32) Gets the element at the specified index.

Methods

Name Description
Add(Object) Adds an item to the collection.
Clear() Removes all items from the collection.
Contains(Object) Determines whether an element is in the collection.
CopyTo(Array, Int32) Copies the entire XmlSelectionRouteNodeValueCollection to a compatible one-dimensional Array, starting at the specified index of the target array.
GetEnumerator() Returns an enumerator that iterates through the collection.
IndexOf(Object) Returns the zero-based index of the first occurrence of a value in the collection or -1 if not found.
Insert(Int32, Object) Inserts an item into the collection at the specified index.
Remove(Object) Removes the first occurrence of a specific object from the collection.
RemoveAt(Int32) Removes the element at the specified index of the collection.

Remarks

This class is part of the internal implementation of the .NET Framework's XML processing. While you can interact with it, it's often not directly instantiated by application code. Instead, it's returned by methods such as SelectNodes on an XmlNode, when the selected nodes are of the types listed in the overview.

The collection holds objects that represent various parts of an XML document. The specific types of nodes it can contain include:

When iterating through an XmlSelectionRouteNodeValueCollection, you will receive instances of these types. You may need to cast the retrieved objects to their specific types to access their properties and methods.

Examples

The following example demonstrates how to use XmlSelectionRouteNodeValueCollection obtained from a selection operation. Note that direct instantiation is less common.


using System;
using System.Xml;

public class Example
{
    public static void Main()
    {
        // Sample XML document
        string xml = @@"<root>
            <element attribute='value1'>Text Node 1</element>
            <!-- This is a comment -->
            <element>Text Node 2</element>
            <![CDATA[This is a CDATA section]]>
        </root>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        // Select nodes that are not elements (attributes, text, comments, cdata)
        XmlNodeList nodes = doc.SelectNodes("//(*|@*|text()|comment()|processing-instruction()|CDATASection())");

        // The result might internally use XmlSelectionRouteNodeValueCollection
        // We'll iterate through the XmlNodeList, which represents the collection
        foreach (XmlNode node in nodes)
        {
            Console.WriteLine($"Node Type: {node.NodeType}, Value: {node.Value}");

            // Demonstrating access to specific types if needed
            if (node.NodeType == XmlNodeType.Attribute)
            {
                XmlAttribute attribute = (XmlAttribute)node;
                Console.WriteLine($"  Attribute Name: {attribute.Name}");
            }
            else if (node.NodeType == XmlNodeType.CdataSection)
            {
                XmlCDataSection cdata = (XmlCDataSection)node;
                Console.WriteLine($"  CDATA Content: {cdata.Data}");
            }
            else if (node.NodeType == XmlNodeType.Comment)
            {
                XmlComment comment = (XmlComment)node;
                Console.WriteLine($"  Comment Text: {comment.InnerText}");
            }
        }
    }
}