NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Enumerating all attributes of an element and adding them to a dictionary using LINQ with Lambda Expressions

Thursday, May 14th, 2009

I have been confronted a number of times lately with the need to record all attributes of an XML element, especially when working with SharePoint XML where the attributes are simply too many to strongly-type
the resulting output.

This can be easily achieved while using LINQ along with a simple Lambda expression.

Without further ado, here is my resolution:

var lists = from list in doc.Descendants("MyElement")
select
{
Attributes = (from attribute in list.Attributes()
select attribute).ToDictionary(
n => n.Name.LocalName, n => n.Value)
};

This will give you a resulting var of elements' XML attributes. This can than be enumerated like any other C# dictionary.

Very useful, and not very hard, but took me a few minutes to figure out nonetheless.

LINQin' Tom Out.

Tags

CSharp LINQ

Related Blogs

Awesome .NET 4: Named and Optional Parameters for Classes and Methods
Validate a Windows Username and Password against Active Directory
Creating an MD5 String Extension method in C#
Code Analysis with NDepend V3
Programatically Retrieving an Assembly's PublicKeyToken through a PowerShell CmdLet

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations