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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:45:28+00:00 2026-05-14T01:45:28+00:00

I have a Repository Class with the following method… public T Single<T>(Predicate<T> expression) {

  • 0

I have a Repository Class with the following method…

public T Single<T>(Predicate<T> expression)
{
    using (var list = (Models.Collectable<T>)System.Xml.Serializer.Deserialize(typeof(Models.Collectable<T>), FileName))
    {
        return list.Find(expression);
    }
}

Where Collectable is defined..

[Serializable]
public class Collectable<T> : List<T>, IDisposable
{
    public Collectable() { }

    public void Dispose() { }
}

And an Item that uses it is defined..

[Serializable]
[System.Xml.Serialization.XmlRoot("Titles")]
public partial class Titles : Collectable<Title>
{
}

The problem is when I call the method, it expects “Collectable” to be the XmlRoot, but the XmlRoot is “Titles” (all of object Title).

I have several classes that are collected in .xml files like this, but it seems pointless to rewrite the basic methods for loading each up when the generic accessors do it – but how can I enforce the proper root name for each file without hard coding methods for each one? The [System.Xml.Serialization.XmlRoot] seems to be ignored.

When called like this…

var titles = Repository.List<Models.Title>(); 

I get the exception

<Titlesxmlns=''> was not expected. 

The XML is formatted such as. ..

<?xml version="1.0" encoding="utf-16"?>
<Titles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Title>
        <Id>442daf7d-193c-4da8-be0b-417cec9dc1c5</Id>
    </Title>
</Titles>

Here is the deserialization code.

  public static T Deserialize<T>(String xmlString)
    {
        System.Xml.Serialization.XmlSerializer XmlFormatSerializer
            = new System.Xml.Serialization.XmlSerializer(typeof(T));

        StreamReader XmlStringReader = new StreamReader(xmlString);

        //XmlTextReader XmlFormatReader = new XmlTextReader(XmlStringReader);

        try
        {
            return (T)XmlFormatSerializer.Deserialize(XmlStringReader);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            XmlStringReader.Close();
        }
    }
  • 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-14T01:45:28+00:00Added an answer on May 14, 2026 at 1:45 am

    It makes no sense for the root of a document to be a collection. It may contain a collection, but it cannot be one.


    Your problem appears to be simpler. You cannot create a serializer using an open generic type (Models.Collectable<T>). Try using typeof(Titles) instead.


    I’m making some assumptions about your repository classes, but would something like this work?

    public class Repository<T>
    {
        protected static TextReader FileName { get; set; }
    
        public static TCollection List<TCollection>()
        {
            var ser = new XmlSerializer(typeof (TCollection));
            return (TCollection) ser.Deserialize(FileName);
        }
    
        public static TItem Single<TItem, TCollection>(Predicate<TItem> expression) 
            where TCollection : IDisposable, IEnumerable<TItem>
        {
            using (var list = List<TCollection>())
            {
                return list.Single(item => expression(item));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T>
I have a git repository which tracks an svn repository. I cloned it using
I have a repository of files which are unrelated to each other but are
I have a repository which I have already cloned from Subversion. I've been doing
We have an SVN repository running on a Windows server, and I want to
I have a subversion repository with the standard layout, i.e. trunk/ and branches/ (and
I have a git repository with remote foo. foo is a web app, is
I have a SVN repository for my project; it keeps code, docs, graphs, etc.
I have a git repository with multiple branches. How can I know which branches
I have a Git repository I store random things in. Mostly random scripts, text

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.