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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:57:43+00:00 2026-06-11T01:57:43+00:00

I am trying to create a parser framework for XML strings containing SQL query

  • 0

I am trying to create a parser framework for XML strings containing SQL query results. The intent is to inherit from generic classes, which are instantiated with the column data types. The included code is for the single-column variety. There will be additional classes for two columns, etc.

I need to be able to specify that the generic type must support the Parse(string) method. How do I do this?

abstract class OneColumnParser<Col1>
{
    abstract string Column1;

    List<Col1> ParseQueryResult(string queryResult)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(queryResult);
        List<Col1> results = new List<Col1>();

        foreach (XmlNode xNode in xDoc.GetElementsByTagName(Column1))
        {
            results.Add(Col1.Parse(xNode.InnerText));
        }
    }
}

When I compile the above, I get “‘Col1’ is a ‘type parameter’, which is not valid in the given context” on the results.Add() line, because I haven’t specified that the type must support the method. But how?

  • 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-06-11T01:57:44+00:00Added an answer on June 11, 2026 at 1:57 am

    Because interfaces can’t have static methods, you can’t (directly) do what you’re asking. Reflection is one way of solving the problem, but it’s only verified at runtime, not enforced by the compiler. E.g.

    abstract class OneColumnParser<TCol>
    {
        private static MethodInfo ParseInfo = typeof(TCol).GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string) }, null);
        abstract string Column1;
    
        static OneColumnParser()
        {
            if (typeof(TCol) != typeof(string) && (ParseInfo == null || !typeof(TCol).IsAssignableFrom(ParseInfo.ReturnType)))
                throw new InvalidOperationException("Invalid type, must contain public static TCol Parse(string)");
        }
    
        private static TCol Parse(string value)
        {
            if (typeof(TCol) == typeof(string))
                return (TCol)(object)value;
            else
                return (TCol)ParseInfo.Invoke(null, new[] { value });
        }
    
        public List<TCol> ParseQueryResult(string queryResult)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(queryResult);
            List<TCol> results = new List<TCol>();
    
            foreach (XmlNode xNode in xDoc.GetElementsByTagName(Column1))
            {
                results.Add(Parse(xNode.InnerText));
            }
    
            return results;
        }
    }
    

    Unlike defining your own interface, this will work on existing types with Parse methods, such as int and DateTime. Update added code so that it will work on string as well.

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

Sidebar

Related Questions

I am trying to create a sort of generic xml parser, like this: Part
I am trying to create a generic formatter/parser combination. Example scenario: I have a
I am trying to create a simple RSS parser using the two frameworks. However
Im trying to create a parser for a small language with commands including labels
Im trying to build a simple XML to Controls parser in my CF application.
I'm trying to create a simple expression parser with antlr 2.7.6 and am getting
Just as the title implies, I am trying to create a parser and trying
I am trying to create a parser for this kind of texts: {{texta1|texta2|texta3} {texta11|texta22}|{textb1|textb2}
I'm trying create a regex that verifies an xml entity name is valid (see
I'm trying to prevent .NET Framework accessing the Web when validating an XML document

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.