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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:27:45+00:00 2026-06-01T13:27:45+00:00

I’m having a hard time trying to create a XmlRepository. The problem here I

  • 0

I’m having a hard time trying to create a XmlRepository. The problem here I have the only choice to do this using XmlSerializer.

Please, check it out. It really a mess my code and frustrating. I wanna know how can I improve this code, I was thinking in creating a singleton but I’m not sure how to continue.

 public interface IRepository<T>
    where T : class
{
    T GetById(object id);
    IEnumerable<T> All();
    void Insert(T entity);
    void Remove(T entity);
    void SaveChanges();
} 

public class XmlRepository : IRepository<Configuration>
{
    public XmlRepository(string filename)
    {
        FileName = filename;
    }

    public XmlRepository(string filename)
    {
        FileName = filename;
    }

    internal string FileName { get; private set; }

    private Configuration GetById(object id)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<Configuration> All()
    {
        return Get();
    }

    public void Insert(Configuration entity)
    {
        var configurations = Get();
        configurations.Add(entity);
        Save(configurations);
    }

    public void Remove(Configuration entity)
    {
        var configurations = Get();
        configurations.Remove(entity);
        Save(configurations);
    }

    private List<Configuration> Get()
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Configuration>), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
            StreamReader myWriter = new StreamReader(FileName);
            var list = serializer.Deserialize(myWriter);
            myWriter.Close();

            return (List<Configuration>)list;
        }
        catch (InvalidOperationException ex)
        {
            throw ex;
        }
    }

    public void Save(object configurations)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(configurations.GetType(), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
            StreamWriter myWriter = new StreamWriter(FileName);
            serializer.Serialize(myWriter, configurations);
            myWriter.Close();
        }
        catch (XmlException ex)
        {
            throw ex;
        } 
    }
}

Any doubt, please let me know.
Thanks a lot

  • 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-01T13:27:47+00:00Added an answer on June 1, 2026 at 1:27 pm

    Instead of reading and writing the file every time you call the repository, I would do the following:

    In the constructor you read the file into a list of Configuration objects. Just like you do in the Get method at the moment. You save this list in a field of the class and use it for all the other methods (add, etc.).

    Your repository does have a SaveChanges method, so this is the ideal place to serialize the configuration back to disk.

    This should be a lot more performant than the current approach, less complex and therefore also less error-prone.

    Edit: Here is a start:

    public class XmlRepository : IRepository<Configuration>
    {
        private readonly List<Configuration> configurations;
    
        public XmlRepository(string filename)
        {
            FileName = filename;
    
            XmlSerializer serializer = new XmlSerializer(typeof(List<Configuration>), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
            using (StreamReader myWriter = new StreamReader(FileName))
            {
                configurations = (List<Configuration>)serializer.Deserialize(myWriter);
                myWriter.Close();
            }
        }
    
        internal string FileName { get; private set; }
    
        public Configuration GetById(object id)
        {
            return (from c in configurations where c.Id == id select c).Single();
        }
    
        public IEnumerable<Configuration> All()
        {
            return configurations;
        }
    
        public void Insert(Configuration entity)
        {
            configurations.Add(entity);
        }
    
        public void Remove(Configuration entity)
        {
            configurations.Remove(entity);
        }
    
        public void SaveChanges()
        {
            XmlSerializer serializer = new XmlSerializer(configurations.GetType(), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
            using (StreamWriter myWriter = new StreamWriter(FileName))
            {
                serializer.Serialize(myWriter, configurations);
                myWriter.Close();
            }
        }
    }
    

    Also some general suggestions

    • Use using for handling files/streams and other resources which needs to be disposed (StreamReader and StreamWriter in this case). This guarantees that the file will be closed even when there is an exception.
    • Don’t catch and rethrow exceptions or if you do it, at least use throw instead of throw ex to preserve the full stack trace.

    Hope this helps!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
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
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post

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.