Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

LINQ to XML Query Extender Method for checking Nulls

Avatar of kruegerste
kruegersteFlag for United States of America asked on
.NET ProgrammingC#
4 Comments1 Solution578 ViewsLast Modified:
Can anybody help me write a short extender method for getting attribute values in an LINQ to XML query?

I found an example that sort of does what I want to and it is only 2 lines of code but it is for elements not attributes and has some other crazy code I don't know about.  See link here > FindSiblingValues function on page:

http://geekswithblogs.net/michelotti/archive/2009/12/29/linq-to-xml-with-hierarchical-xml-optional-elements-and-namespaces.aspx

In the code snippet below, we are checking for nulls with inline if statements.  I think these will end up getting expanded and changing overtime so it would be nice to have an extender function that is called, pass it the attribute name and have it return the value (null or otherwise).  

Then we can maintain one function for handling the nulls.  

Please take a look at my existing Query below and let me know if you have any ideas.

thanks.
List<PersonsSportsInfo> personsSportsInfo =
                    (from persons in Root.Descendants("player")
                     let personsMetaData = persons.Elements("player-metadata").FirstOrDefault()
                     let firstName = (personsMetaData.Element("name") == null) ? "" : (personsMetaData.Element("name").Attribute("first") == null) ? "" : personsMetaData.Element("name").Attribute("first").Value
                     let lastName = (personsMetaData.Element("name") == null) ? "" : (personsMetaData.Element("name").Attribute("last") == null) ? "" : personsMetaData.Element("name").Attribute("last").Value
                     let careerPhase = persons.Descendants("career-phase")
                     select new PersonsSportsInfo
                     {
                         Key = (personsMetaData == null) ? null : (personsMetaData.Attribute("player-key") == null) ? null : personsMetaData.Attribute("player-key").Value,
                         UniformNumber = (personsMetaData == null) ? null : (personsMetaData.Attribute("uniform-number") == null) ? null : personsMetaData.Attribute("uniform-number").Value,
                         PositionRegular = (personsMetaData == null) ? null : (personsMetaData.Attribute("position-regular") == null) ? null : personsMetaData.Attribute("position-regular").Value,
                         Height = (personsMetaData == null) ? null : (personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value,
                         Weight = (personsMetaData == null) ? null : (personsMetaData.Attribute("weight") == null) ? null : personsMetaData.Attribute("weight").Value,
                         Status = (personsMetaData == null) ? null : (personsMetaData.Attribute("status") == null) ? null : personsMetaData.Attribute("status").Value,
                         DOB = (personsMetaData == null) ? null : (personsMetaData.Attribute("date-of-birth") == null) ? null : personsMetaData.Attribute("date-of-birth").Value,
                         Health = (personsMetaData == null) ? null : (personsMetaData.Attribute("health") == null) ? null : personsMetaData.Attribute("health").Value,
                         Caliber = (personsMetaData == null) ? null : (personsMetaData.Attribute(xts + "caliber") == null) ? null : personsMetaData.Attribute(xts + "caliber").Value,
                         FirstName = firstName,
                         LastName = lastName,
                         FullName = firstName + " " + lastName,
                         PositionDepth = (personsMetaData == null) ? null : (personsMetaData.Element("player-metadata-american-football") == null) ? null : (personsMetaData.Element("player-metadata-american-football").Attribute(xts + "position-depth") == null) ? "" : personsMetaData.Element("player-metadata-american-football").Attribute(xts + "position-depth").Value,
                         ProExperience = careerPhase
                             .Where(c => c.Attribute("phase-type").Value == "professional")
                             .Select(c => c.Attribute("duration").Value).FirstOrDefault(),
                         CollegeName = careerPhase
                             .Where(c => c.Attribute("phase-type").Value == "college")
                             .Select(c => c.Attribute("name").Value).FirstOrDefault(),
                    }).ToList();
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers