Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get attributes Name and Value of element in C# through System.Linq

I have one custom config file.

<Students>
 <student>
   <Detail Name="abc" Class="1st Year">
       <add key="Main" value="web"/>
       <add key="Optional" value="database"/>
   </Detail>
 </student>
</Students>

I read this file through the IConfigurationHandler interface implementation. When I read the childNode attributes of Detail element. It return me below result into Immediate Window of IDE.

elem.Attributes.ToObjectArray()

{object[2]}
    [0]: {Attribute, Name="key", Value="Main"}
    [1]: {Attribute, Name="value", Value="web"}

When I try to write on Console

 Console.WriteLine("Value '{0}'",elem.Attributes.ToObjectArray());

it does return me

Value : 'System.Configuration.ConfigXmlAttribute'

elem.Attributes.Item(1) method gives me the Name and Value detail but here I need to pass the index value of attribute which I don't know currently.

I want to get Name and value of attribute through LINQ query and individual display on Console for each childNode attribute as follows:

Value : Name="Key" and Value="Main"
        Name="value", Value="web"

How can I achieve that?

like image 770
Saroop Trivedi Avatar asked May 24 '12 07:05

Saroop Trivedi


People also ask

How do I get custom attribute values?

To get custom attribute values with jQuery, we can use the data method. to add a div with the data-something custom attribute. We select the div with $ . And then we call data with the part of the attribute name after data- to get its value.

What is attribute name and value?

The attribute identifier, also called attribute name, is a string that identifies an attribute. An attribute value is the content of the attribute and its type is not restricted to that of string. You use an attribute name when you want to specify a particular attribute for either retrieval, searches, or modification.

How do you use getAttribute?

How it works: First, select the link element with the id js using the querySelector() method. Second, get the target attribute of the link by calling the getAttribute() of the selected link element. Third, show the value of the target on the Console window.


1 Answers

If you want to use this Xml Library you can get all the students and their details with this code:

XElement root = XElement.Load(file); // or .Parse(string)
var students = root.Elements("student").Select(s => new
{
    Name = s.Get("Detail/Name", string.Empty),
    Class = s.Get("Detail/Class", string.Empty),
    Items = s.GetElements("Detail/add").Select(add => new
    {
        Key = add.Get("key", string.Empty),
        Value = add.Get("value", string.Empty)
    }).ToArray()
}).ToArray();

Then to iterate over them use:

foreach(var student in students)
{
    Console.WriteLine(string.Format("{0}: {1}", student.Name, student.Class));
    foreach(var item in student.Items)
        Console.WriteLine(string.Format("  Key: {0}, Value: {1}", item.Key, item.Value));
}
like image 58
Chuck Savage Avatar answered Sep 29 '22 16:09

Chuck Savage