Table of Contents

HierarchyDecoratorExtensions Class

Definition

Namespace
Cuemon.Xml
Assemblies
Cuemon.Xml.dll
Source
src/Cuemon.Xml/Extensions/HierarchyDecoratorExtensions.cs

Extension methods for the IHierarchy<T> interface hidden behind the IDecorator<T> interface.

public static class HierarchyDecoratorExtensions
Inheritance
object
HierarchyDecoratorExtensions

Examples

HierarchyDecoratorExtensions provides extension methods on Decorator.Enclose for inspecting XML serialization metadata on IHierarchy<object> nodes. This example creates a Hierarchy<object> tree with a Person node (annotated with [XmlRoot("person")]) and an Address child node, then uses decorator methods to call GetXmlQualifiedEntity, TryGetXmlRootAttribute, TryGetXmlElementAttribute, TryGetXmlTextAttribute, TryGetXmlAttributeAttribute, IsNodeEnumerable, HasXmlIgnoreAttribute, and OrderByXmlAttributes. Console output displays the qualified entity local name, boolean results for attribute detection, and the count of ordered nodes.

using System;
using Cuemon;
using Cuemon.Extensions.Runtime;
using Cuemon.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace MyApp.Examples;

public static class HierarchyDecoratorExtensionsExample
{
    public static void Demonstrate()
    {
        var hierarchy = new Hierarchy<object>();
        var root = hierarchy.Add(new Person { Name = "Alice" });
        var child = root.Add(new Address { City = "Paris" });

        var decorator = Decorator.Enclose((IHierarchy<object>)child);
        var rootDecorator = Decorator.Enclose((IHierarchy<object>)root);
        var qualifiedEntity = decorator.GetXmlQualifiedEntity();
        var hasRoot = rootDecorator.TryGetXmlRootAttribute(out _);
        var hasElement = decorator.TryGetXmlElementAttribute(out _);
        var hasText = decorator.TryGetXmlTextAttribute(out _);
        var hasAttribute = decorator.TryGetXmlAttributeAttribute(out _);
        var ordered = Decorator.Enclose((IEnumerable<IHierarchy<object>>)new[] { root, child }).OrderByXmlAttributes<object>().ToList();

        Console.WriteLine(qualifiedEntity.LocalName);
        Console.WriteLine(hasRoot);
        Console.WriteLine(hasElement || hasText || hasAttribute);
        Console.WriteLine(decorator.IsNodeEnumerable());
        Console.WriteLine(decorator.HasXmlIgnoreAttribute());
        Console.WriteLine(ordered.Count);
    }

    [XmlRoot("person")]
    private sealed class Person
    {
        public string Name { get; set; }
    }

    private sealed class Address
    {
        public string City { get; set; }
    }
}

Methods

Name Description
GetXmlQualifiedEntity(IDecorator<IHierarchy<object>>, XmlQualifiedEntity)

Resolves an XmlQualifiedEntity from either the specified qualifiedEntity or from the underlying IHierarchy<T> of the decorator.

HasXmlIgnoreAttribute(IDecorator<IHierarchy<object>>)

Determines whether the underlying IHierarchy<T> of the decorator implements XmlIgnoreAttribute.

IsNodeEnumerable(IDecorator<IHierarchy<object>>)

Determines whether the underlying IHierarchy<T> of the decorator implements either IEnumerable or IEnumerable<T> and is not a string.

OrderByXmlAttributes<T>(IDecorator<IEnumerable<IHierarchy<T>>>)

Orders a sequence of IHierarchy<T> from the underlying sequence of IHierarchy<T> values of the decorator by nodes having an XmlAttributeAttribute decoration.

TryGetXmlAttributeAttribute(IDecorator<IHierarchy<object>>, out XmlAttributeAttribute)

Attempts to get an XmlAttributeAttribute from the underlying IHierarchy<T> of the decorator.

TryGetXmlElementAttribute(IDecorator<IHierarchy<object>>, out XmlElementAttribute)

Attempts to get an XmlElementAttribute from the underlying IHierarchy<T> of the decorator.

TryGetXmlRootAttribute(IDecorator<IHierarchy<object>>, out XmlRootAttribute)

Attempts to get an XmlRootAttribute from the underlying IHierarchy<T> of the decorator.

TryGetXmlTextAttribute(IDecorator<IHierarchy<object>>, out XmlTextAttribute)

Attempts to get an XmlTextAttribute from the underlying IHierarchy<T> of the decorator.

See Also