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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:13:45+00:00 2026-05-20T03:13:45+00:00

I have a bunch of different DTO classes. They are being serialized into an

  • 0

I have a bunch of different DTO classes. They are being serialized into an XML string at one point and shot over to client-side of the web app. Now when the client shoots back an XML string, I need to deserialize it back to an instance of the DTO class that it represents. The problem is that I want to make it generic and possibly a function which takes in an xml string and spits out an object of a type. Something like a long these lines:

public sometype? Deserialize (string xml)
{
//some code here
return objectFromXml;
}

EDIT: Horrible example! I just contradicted myself!

I cannot do the following:

Person person = Deserialize(personXmlStringFromClient);

because I don’t know that personXmlStringFromClient is a representation of Person DTO object instance.

I don’t know what serialized object is given to me and that seems to be my problem here. I’ve been reading about reflection and other techniques which involve sticking the type into the xml so that deserializer knows what to do with it. I can’t seem to pull it all together into one working piece. Also, in pretty much most examples, the author knows what type there will be after deserialization. Any suggestion is welcome! If I need to do something special with the serialization process, please share that too.

  • 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-20T03:13:46+00:00Added an answer on May 20, 2026 at 3:13 am

    You can use a generic:

        public T Deserialize<T>(string input)
            where T : class
        {
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
    
            using (StringReader sr = new StringReader(input))
                return (T)ser.Deserialize(sr);
        }
    

    If you don’t know which type it will be, I assume you have a fixed number of possible types, and you could try deserializing to each one until you don’t encounter an exception. Not great, but it would work.

    Or, you could inspect the start of the xml for the outer object name and hopefully be able to determine the type from there. This would vary depending on what the xml looks like.

    Edit: Per your edit, if the caller knows the type that they are passing, could they supply the fully qualified typename as a string as an additional parameter to the service?

    If so, you could do this:

        Type t = Type.GetType(typeName);
    

    and change the Deserialize method to be like this:

    public object Deserialize(string input, Type toType)
    {
        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(toType);
    
        using (StringReader sr = new StringReader(input))
            return ser.Deserialize(sr);
    }
    

    However, this only gets you an object… If all of the types in question implement a common interface, you could deserialize as above but change the return type to the interface (and cast to it in the return statement)

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

Sidebar

Related Questions

I have a bunch of third-party Java classes that use different property names for
We have a long page that contains a bunch of different form elements. One
I have a bunch of classes under these four different namespaces: a.b.c.x a.b.c.y a.b.c.z
I have a bunch of different Feature classes which calculate image features. I have
I have a bunch of different objects that are commonly edited in the same
I have looked at this a bunch of different ways and with what little
In my DB I have a bunch of listings with different city names. Im
Let's say that I have a bunch of class instances that serve different purposes,
I have a bunch of code of the form: someVector.push_back(Foo(some name, 1.0, 3.1415926); someVector.push_back(Foo(different
I have a bunch of html files with different file-names, that I need to

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.