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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:52:21+00:00 2026-06-07T00:52:21+00:00

All — I have these two methods in one of my classes: public static

  • 0

All —
I have these two methods in one of my classes:

public static void DeSerializeFromXml(out cDBData db, string pathToXML)
{
    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDBData));
        db = (cDBData)ser.Deserialize(reader);
    }
}

public static void DeSerializeFromXml(out cDatabases db, string pathToXML)
{
    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDatabases));
        db = (cDatabases)ser.Deserialize(reader);
    }
}

and they work fine but I was wondering why can’t I create an method overload based upon the return type of the method. I thought I read that this was possible somewhere but I am obviously wrong because it doesn’t work:

public static cDBData DeSerializeFromXml(string pathToXML)
{
    cDBData db;

    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDBData));
        db = (cDBData)ser.Deserialize(reader);
    }
    return db;
}

public static cDatabases DeSerializeFromXml(string pathToXML)
{
    cDatabases db;

    using (XmlTextReader reader = new XmlTextReader(pathToXML))
    {
        XmlSerializer ser = new XmlSerializer(typeof(cDatabases));
        db = (cDatabases)ser.Deserialize(reader);
    }
    return db;
}

Thanks for your thoughtful responses

Thanks to dlev, here are the final solutions

    public static T DeSerializeFromXml<T>(string pathToXML)
    {
        T db;

        using (XmlTextReader reader = new XmlTextReader(pathToXML))
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            db = (T)ser.Deserialize(reader);
        }
        return db;
    }

    public static void SerializeToXml<T>(T db, string pathToXML)
    {
        using (var fileStream = new FileStream(pathToXML, FileMode.Create))
        {
            var ser = new XmlSerializer(typeof(T));
            ser.Serialize(fileStream, db);
        }
    }

I wanted to get these posted before the Search Police close down this question.

  • 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-07T00:52:24+00:00Added an answer on June 7, 2026 at 12:52 am

    You can’t do that because C# simply does not support that kind of overloading. You can achieve a similar effect by making the method generic, and returning the the generic parameter type:

    public static T DeSerializeFromXml<T>(string pathToXML)
    {
        T db;
    
        using (XmlTextReader reader = new XmlTextReader(pathToXML))
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            db = (T)ser.Deserialize(reader);
        }
        return db;
    }
    

    You can then call the functions like so:

    DeSerializeFromXml<cDatabases>(pathToXml);
    

    While that’s not quite what you wanted, it does have the benefit of only requiring a single method.

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

Sidebar

Related Questions

All- I have two spinners side by side. I want it to be so
So to start, I have an array of XML files. These files need to
All, I have some script tags that are not working in Wordpress. If I
All, Say I have the following bit of code: select: function(start, end, allDay) {
All, I have the following date: Wed Feb 01 2012 09:30:00 GMT-0600 (Central Standard
All, I have the following code: $fetch = mysql_query(SELECT * FROM calendar_events where event_status='booked'
All the examples of use and tutorials I have found on beginBackgroundTaskWithExpirationHandler: show how
All, I have a form with a MultiValueField that almost works. It uses a
All, I have a shell script on Mac OS X which uses the mdls
All I have found about this is that there are inconsistencies among different web

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.