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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:09:06+00:00 2026-05-27T02:09:06+00:00

This is related to a prior question of mine C# Generic List conversion to

  • 0

This is related to a prior question of mine C# Generic List conversion to Class implementing List<T>

I have the following code:

public abstract class DataField
{
    public string Name { get; set; }
}

public class DataField<T> : DataField
{
    public T Value { get; set; }
}

public static List<DataField> ConvertXML(XMLDocument data) {  
     result = (from d in XDocument.Parse(data.OuterXML).Root.Decendendants()
                      select new DataField<string>
                      {
                          Name = d.Name.ToString(),
                          Value = d.Value
                      }).Cast<DataField>().ToList();  
    return result;
}

This works however I would like to be able to modify the select portion of the LINQ query to be something like this:

select new DataField<[type defined in attribute of XML Element]>
{
  Name = d.Name.ToString(),
  Value = d.Value
}

Is this just a poor approach? is it possible? 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-27T02:09:07+00:00Added an answer on May 27, 2026 at 2:09 am

    Here is a working solution: (You must specify fully qualified type names for your Type attribute otherwise you have to configure a mapping somehow…)

    I used the dynamic keyword, you can use reflection to set the value instead if you do not have C# 4…

    public static void Test()
    {
        string xmlData = "<root><Name1 Type=\"System.String\">Value1</Name1><Name2 Type=\"System.Int32\">324</Name2></root>";
    
        List<DataField> dataFieldList = DataField.ConvertXML(xmlData);
    
        Debug.Assert(dataFieldList.Count == 2);
        Debug.Assert(dataFieldList[0].GetType() == typeof(DataField<string>));
        Debug.Assert(dataFieldList[1].GetType() == typeof(DataField<int>));
    }
    
    public abstract class DataField
    {
        public string Name { get; set; }
    
        /// <summary>
        /// Instanciate a generic DataField<T> given an XElement
        /// </summary>
        public static DataField CreateDataField(XElement element)
        {
            //Determine the type of element we deal with
            string elementTypeName = element.Attribute("Type").Value;
            Type elementType = Type.GetType(elementTypeName);
    
            //Instanciate a new Generic element of type: DataField<T>
            dynamic dataField = Activator.CreateInstance(typeof(DataField<>).MakeGenericType(elementType));
            dataField.Name = element.Name.ToString();
    
            //Convert the inner value to the target element type
            dynamic value = Convert.ChangeType(element.Value, elementType);
    
            //Set the value into DataField
            dataField.Value = value;
    
            return dataField;
        }
    
        /// <summary>
        /// Take all the descendant of the root node and creates a DataField for each
        /// </summary>
        public static List<DataField> ConvertXML(string xmlData)
        {
            var result = (from d in XDocument.Parse(xmlData).Root.DescendantNodes().OfType<XElement>()
                          select CreateDataField(d)).ToList();
    
            return result;
        }
    }
    
    public class DataField<T> : DataField
    {
        public T Value { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched here, GooBingHooVista'd the world and read this related question for VS
This is related to another open question of mine . While there aren't any
So this is related to a prior question I posted - how to remove
This question is related to my prior question: Is READYSTATE_LOADED across browsers? So I
This question is an extension of this Related question . Taking Derick's advice, I
I found this related question: How do I use composition with inheritance? I would
This is related to this question but with a different take. In Ubuntu, I
This is related to OpenId authentication. I have implemented the google and yahoo openId
This is related to my other question but I am openning a new one
This is related to this question I asked earlier about syntax highlighting user-defined blocks

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.