Link to home
Start Free TrialLog in
Avatar of kruegerste
kruegersteFlag for United States of America

asked on

Handling Attribute Nulls in LINQ to XML Query

What is the best way to handle null Attributes when assigning the value in an LINQ query?

See the code snippet below, many examples.  We are currently using inline if statements to check if the attribute exists and then handle if it does or not. For example:

 Height = (personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value,

If this is the best way, anybody have any examples of an extender function  that would cut down on the amount of code for checking nulls inside the query?  Something that could be a little easier maintained?

Thanks,
Sk
List<PersonsSportsInfo> personsSportsInfo =
                    (from persons in Root.Descendants("player")
                     let personsMetaData = persons.Elements("player-metadata").First()
                     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.Attribute("player-key") == null) ? null : personsMetaData.Attribute("player-key").Value,
                         UniformNumber = (personsMetaData.Attribute("uniform-number") == null) ? null : personsMetaData.Attribute("uniform-number").Value,
                         PositionRegular = (personsMetaData.Attribute("position-regular") == null) ? null : personsMetaData.Attribute("position-regular").Value,
                         Height = (personsMetaData.Attribute("height") == null) ? null : personsMetaData.Attribute("height").Value,
                         Weight = (personsMetaData.Attribute("weight") == null) ? null : personsMetaData.Attribute("weight").Value,
                         Status = (personsMetaData.Attribute("status") == null) ? null : personsMetaData.Attribute("status").Value,
                         DOB = (personsMetaData.Attribute("date-of-birth") == null) ? null : personsMetaData.Attribute("date-of-birth").Value,
                         Health = (personsMetaData.Attribute("health") == null) ? null : personsMetaData.Attribute("health").Value,
                         Caliber = (personsMetaData.Attribute(xts + "caliber") == null) ? null : personsMetaData.Attribute(xts + "caliber").Value,
                         FirstName = firstName,
                         LastName = lastName,
                         FullName = firstName + " " + lastName,
                         PositionDepth = (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();

Open in new window

Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

You could certainly make the code more palatable by defining a function

object GetValue(DataTypeOfAttributeEntry foo) {
      return (foo == null) ? null : foo.Value;
}

and using
 Key = GetValue(personsMetaData.Attribute("player-key"));

etc., instead.

(if you these values are always strings, it could be string GetValue)

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kruegerste

ASKER

Awesome example, thanks looks great.  Hope you are feeling better soon, thanks for taking time, it wasn't necessary.
Not a problem, glad to help out.  ;=)