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

  • Home
  • SEARCH
  • 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 4064358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:50:44+00:00 2026-05-20T15:50:44+00:00

I am trying to figure out how to handle nodes that do not exist

  • 0

I am trying to figure out how to handle nodes that do not exist for all of my “card” elements. I have the following linq query:

    FinalDeck = (from deck in xmlDoc.Root.Element("Cards")
                    .Elements("Card")
                    select new CardDeck
                    {
                        Name = deck.Attribute("name").Value,
                        Image = deck.Element("Image").Attribute("path").Value,
                        Usage = (int)deck.Element("Usage"),
                        Type = deck.Element("Type").Value,
                        Strength = (int)deck.Element("Ability") ?? 0
                    }).ToList();  

with the Strength item, I had read another posting that the ?? handles the null. I am getting the following error though:

Operator ‘??’ cannot be applied to operands of type ‘int’ and ‘int’

How do I handle this issue?

Thanks!

  • 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-20T15:50:45+00:00Added an answer on May 20, 2026 at 3:50 pm

    Rather than use the Value property, cast to string… and for the int, cast to int? instead. The user defined conversions to nullable types will return null if the source XAttribute/XElement is null:

    FinalDeck = (from deck in xmlDoc.Root.Element("Cards")
                    .Elements("Card")
                    select new CardDeck
                    {
                        Name = (string) deck.Attribute("name"),
                        Image = (string) deck.Element("Image").Attribute("path"),
                        Usage = (int?) deck.Element("Usage"),
                        Type = (string) deck.Element("Type"),
                        Strength = (int?) deck.Element("Ability") ?? 0
                    }).ToList();  
    

    Note that this won’t help for the case where the Image element is missing, as then it’ll try to dereference a null element to find the path attribute. Let me know if you want a workaround for that, but it’ll be a bit of a pain, relatively speaking.

    EDIT: You can always create an extension method for this yourself:

    public static XAttribute NullSafeAttribute(this XElement element, XName name)
    {
        return element == null ? null : element.Attribute(name);
    }
    

    Then call it like this:

    Image = (string) deck.Element("Image").NullSafeAttribute("path"),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the best way to handle a backgroundworker that is
I am trying to figure out how to accomplish the following: Handle client-side click
I'm trying to figure out why the control does not honor ZIndex. Example 1
I'm trying to figure out how to detect the type of credit card based
Trying to figure out an equation to get the current group a page would
In trying to figure out this problem (which is still unsolved and I still
I'm trying to figure out how big a certain database would be (it hasn't
I'm basically trying to figure out the simplest way to perform your basic insert
I've been trying to figure out a regex to allow me to search for
I'm trying to figure out what a Java applet's class file is doing under

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.