Answers for "xml reader attributes"

C#
0

xml reader attributes

// Get Xml node attribute keys and values
static IEnumerable<(string key, string value)> GetNodeAttributes(XmlReader reader)
{
    for (int i = 0; i < reader.AttributeCount; i++)
    {
        reader.MoveToAttribute(i);
        yield return (reader.Name, reader.Value);
    }
}

// This can be used with a Linq selector
//to retrieve specific value from the attributes
static string GetAttributeValue(this IEnumerable<(string key, string value)> attributes, string attribute) =>
    attributes.Where(x => x.key == attribute).ToList()[0].value;

// Combine these methods like so to get a attribute neatly.
string value = GetNodeAttributes(reader).GetAttributeValue("name");
Posted by: Guest on September-07-2021

C# Answers by Framework

Browse Popular Code Answers by Language