Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:54:40+00:00 2026-05-27T11:54:40+00:00

I have a model class public class Item { public string Name {get; set;}

  • 0

I have a model class

public class Item
{
    public string Name {get; set;}
    public string Desc {get; set;}
}

I would query my XML document

List<Item> item = xmlDoc.Descendants()
    .Select(o => new Item { 
        Name = o.Attribute("name").Value, 
        Desc = o.Attribute("desc").Value
    }).ToList(); 

However, the attribute desc may or may not be present for each item. The above LINQ works if the attribute desc is present, but will cause an exception if not.

If it does not exist I would like for the LINQ query to just assign null to the Desc field in the new Item object. Thank you for any suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:54:40+00:00Added an answer on May 27, 2026 at 11:54 am

    The right way to do this is with the conversion operators:

    Name = (string) o.Attribute("name"),
    Desc = (string) o.Attribute("desc")
    

    why is this the preferred way? Firstly, it is easy; and secondly, it works correctly for other types:

    Count = (int?) o.Attribute("count"),
    When = (DateTime?) o.Attribute("when")
    

    In particular, these also apply the correct xml encoding rules for each data type, rather than using a culture-specific DateTime.Parse / int.Parse etc. Lots of subtle things to not want to remember!

    Note that if you want to assert that the attribute exists, the non-Nullable<T> versions work too:

    Size = (int) o.Attribute("size"),
    Created = (DateTime) o.Attribute("created")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model public class Foo{ public int Id{get;set;} public string Name {get;
I have a custom class: public class Person { public String Name { get;
I have a model item public class EntryInputModel { ... [Required(ErrorMessage = Description is
So I have the following model: public class Person { public String FirstName {
public class SomePropertyClass{ public string VarA{get;set;} public string VarB{get;set;} } SomePropertyClass v1 = new
I have a model class like following public class ProductModel { string ProductName {
In my model I have class: public class Heaviness { public int ID {get;
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a class Similar to this public class Model { public TimeSpan Time1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.