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 6938111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:27:03+00:00 2026-05-27T12:27:03+00:00

Here is my code, which works perfectly except for one case: when I have

  • 0

Here is my code, which works perfectly except for one case: when I have an xAttribute of bool, so xAttribute.Value == 1. In this situation, Convert doesn’t work for a numeric type.

Normally I would just use output = (bool) xAttribute, which works; but in this method I have a generic type, so I want to use that generic type along the lines of output = (T) xAttribute. How can I do this?

    public static bool TryGetValueFromAttribute<T>(
        this XElement element, 
        String attName, 
        out T output, 
        T defaultValue)
    {
        var xAttribute = element.Attribute(attName);
        if (xAttribute == null)
        {
            output = defaultValue;
            return false;
        }

        output = (T)Convert.ChangeType(xAttribute.Value, typeof(T));
        return true;
    }
  • 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-27T12:27:03+00:00Added an answer on May 27, 2026 at 12:27 pm

    I just used the XmLConvert. Is an easy workaround. It even works with 0 and 1

    Regards

            public static bool TryGetValueFromAttribute<T>(this XElement element, String attName, out T output, T defaultValue)
        {
            var xAttribute = element.Attribute(attName);
            if (xAttribute == null)
            {
                output = defaultValue;
                return false;
            }
    
            if(typeof(T) == typeof(bool))
            {
                object value = XmlConvert.ToBoolean(xAttribute.Value);
                output = (T) value;
    
                return true;
            }
    
            output = (T)Convert.ChangeType(xAttribute.Value, typeof(T));
            return true;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code here which works perfectly in firefox but not in chrome
I have this code here, which is intended to allow any type of arguments:
I have the following code which works perfectly fine in my php file and
I have the following code which works perfectly but I need an onblur statement
Here is my code, which works perfectly in all but ie! The active simply
I am trying to understand this inline assembly code which comes from _hypercall0 here
here is my current line of code: $(<li>).text($(this).text()).appendTo(#theList); Which results in <ul id=theList> <li>blah</li>
I am building a phonegap app for iOS which works perfectly and beautifully except
I have a bit of CSS3 animation which works perfectly in all the browser
I have some code that I have created for an OnChange event which works

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.