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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:47:05+00:00 2026-05-20T01:47:05+00:00

I’ve looked for a similar question, but the only one that was close didn’t

  • 0

I’ve looked for a similar question, but the only one that was close didn’t help me in the end.

I have an XML file that looks like this:

<Fields>
    <Field name="abc" value="2011-01-01" />
    <Field name="xyz" value="" />
    <Field name="tuv" value="123.456" />
</Fields>

I’m trying to use Linq-to-XML to get the values from these fields. The values can be of type Decimal, DateTime, String and Int32. I was able to get the fields one by one using a relatively simple query. For example, I’m getting the ‘value’ from the field with the name ‘abc’ using the following:

private DateTime GetValueFromAttribute(IEnumerable<XElement> fields, String attName)
{
    return (from field in fields
           where field.Attribute("name").Value == "abc"
           select (DateTime)field.Attribute("value")).FirstOrDefault()
}

this is placed in a separate function that simply returns this value, and everything works fine (since I know that there is only one element with the name attribute set to ‘abc’). however, since I have to do this for decimals and integers and dates, I was wondering if I can make a generic function that works in all cases. this is where I got stuck. here’s what I have so far:

private T GetValueFromAttribute<T>(IEnumerable<XElement> fields, String attName)
{
    return (from field in fields
            where field.Attribute("name").Value == attName
            select (T)field.Attribute("value").Value).FirstOrDefault();
}

this doesn’t compile because it doesn’t know how to convert from String to T. I tried boxing and unboxing (i.e. select (T) (Object) field.Attribute("value").Value but that throws a runtime Specified cast is not valid exception as it’s trying to convert the String to a DateTime, for instance.

Is this possible in a generic function? can I put a constraint on the generic function to make it work? or do I have to have separate functions to take advantage of Linq-to-XML’s explicit cast operators?

  • 1 1 Answer
  • 2 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-20T01:47:06+00:00Added an answer on May 20, 2026 at 1:47 am

    You can try do it this way:

    public static T GetValueFromAttribute<T>(IEnumerable<XElement> fields, String attName)
    {
      return (from field in fields
              where field.Attribute("name").Value == attName
              select (T)Convert.ChangeType(field.Attribute("value").Value, typeof(T))).FirstOrDefault();
    }
    

    It will work for DateTime, int etc (all types that implement IConvertible).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.