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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:53:51+00:00 2026-05-13T19:53:51+00:00

I’m trying out Generics and I had this (not so) great idea of creating

  • 0

I’m trying out Generics and I had this (not so) great idea of creating an XMLSerializer class. The code I pieced together is below:

public class Persist<T>
{
    private string _path;
    public Persist(string path) {
        this._path = path;
    }
    public void save(T objectToSave)
    {
        XmlSerializer s = new XmlSerializer(typeof(T));
        TextWriter w = new StreamWriter(this._path);
        try { s.Serialize(w, objectToSave); }
        catch (InvalidDataException e) { throw e; }
        w.Close(); w.Dispose();
    }
    public T load()
    {
        XmlSerializer s = new XmlSerializer(typeof(T));
        TextReader r = new StreamReader(this._path);
        T obj;
        try { obj = (T)s.Deserialize(r); }
        catch (InvalidDataException e) { throw e; }
        r.Close(); r.Dispose();
        return obj;
    }
}

Here’s the problem: It works fine on Persist<List<string>> or Persist<List<int>> but not on Persist<List<userObject>> or any other custom (but serializable) objects. userObject itself is just a class with two {get;set;} properties, which I have serialized before.

I’m not sure if the problems on my Persist class (generics), XML Serialization code, or somewhere else 🙁 Help is very much appreciated~

Edit:

code for userObject

public class userObject
{
    public userObject(string id, string name)
    {
        this.id = id;
        this.name = name;
    }
    public string id { get;private set; }
    public string name { get;set; }
}
  • 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-13T19:53:51+00:00Added an answer on May 13, 2026 at 7:53 pm

    Looks to me like your code should just work – even though it does have a few flaws.

    EDIT: Your userObject class isn’t serializable. Xml serialization only works on types with a public, parameterless constructor – the current class won’t work. Also, you should really rewrite your code to avoid explicit calls to .Close() or .Dispose() and instead prefer using where possible – as is, you might get random file locking if at any point during serialization an error occurs and your method terminates by exception – and thus doesn’t call .Dispose().

    Personally, I tend to use a just-for-serialization object hierarchy that’s just a container for data stored in xml and avoids any behavior – particularly side effects. Then you can use a handly little base class that makes this simple.

    What I use in my projects is the following:

    public class XmlSerializableBase<T> where T : XmlSerializableBase<T>
    {
        static XmlSerializer serializer = new XmlSerializer(typeof(T));
        public static T Deserialize(XmlReader from) { return (T)serializer.Deserialize(from); }
        public void SerializeTo(Stream s) { serializer.Serialize(s, this); }
        public void SerializeTo(TextWriter w) { serializer.Serialize(w, this); }
        public void SerializeTo(XmlWriter xw) { serializer.Serialize(xw, this); }
    }
    

    …which caches the serializer in a static object, and simplifies usage (no generic type-paramenters needed at call-locations.

    Real-life classes using it:

    public class ArtistTopTracks {
        public string name;
        public string mbid;//always empty
        public long reach;
        public string url;
    }
    
    [XmlRoot("mostknowntracks")]
    public class ApiArtistTopTracks : XmlSerializableBase<ApiArtistTopTracks> {
            [XmlAttribute]
            public string artist;
            [XmlElement("track")]
            public ArtistTopTracks[] track;
    }
    

    Sample serialization calls:

    using (var xmlReader = XmlReader.Create([...])) 
        return ApiArtistTopTracks.Deserialize(xmlReader);
    //[...]
    
    ApiArtistTopTracks toptracks = [...];
    toptracks.SerializeTo(Console.Out);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is

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.