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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:16:07+00:00 2026-05-13T15:16:07+00:00

I have the following model: public class ABaseObject { private Guid id = Guid.NewGuid();

  • 0

I have the following model:

public class ABaseObject
{
    private Guid id = Guid.NewGuid();

    public ABaseObject()
    {
    }

    public Guid ID { get { return id; } }
}

public class ADerivedObject : ABaseObject
{
    public ADerivedObject()
    {
    }

    public string Name { get; set; }
}

public class AObjectCollection<T>
{
    private List<T> items = new List<T>();

    public AObjectCollection()
    {
    }

    public IEnumerable<T> Items { get { return items; } }
    public void Add(T item)
    {
        items.Add(item);
    }

    public void Save(string filePath)
    {
        FileStream writer = new FileStream(filePath, FileMode.Create);
        DataContractSerializer s = new DataContractSerializer(typeof(T));
        s.WriteObject(writer, this);
        writer.Close();
    }

    public void Load(string filePath)
    {
        FileStream fs = new FileStream(filePath, FileMode.Open);
        XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
        DataContractSerializer ser = new DataContractSerializer(typeof(T));

        // Deserialize the data and read it from the instance.
        T deserializedObj = (T)ser.ReadObject(reader, true);
        reader.Close();
        fs.Close();
    }
}

So basically I want to be able to do the following:

var der = new ADerivedObject();
der.Name = "Test";
var col = new AObjectCollection<ADerivedObject>();
col.Add(der);
col.Save("C:\MyCollection.xml");
...
var col2 = new AObjectCollection<ADerivedObject>();
col2.Load("C:\MyCollection.xml");

When serialized it should look something like:

<Collection>
    <Item>
        <ID></ID>
        <Name></Name>
    </Item>
</Collection>

I have played around with DataContracts and XmlSerializer but I can’t quite seem to find a way to do it.

  • 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-13T15:16:07+00:00Added an answer on May 13, 2026 at 3:16 pm

    This code:

    public class ABaseObject
    {
        public ABaseObject() { }
        public Guid ID { get; set;}
    }
    
    [XmlType("Item")]
    public class ADerivedObject : ABaseObject
    {
        public ADerivedObject() {}
        public string Name { get; set; }
    }
    
    [XmlType("Collection")]
    public class AObjectCollection<T>
    {
        private System.Xml.Serialization.XmlSerializerNamespaces ns;
        private System.Xml.Serialization.XmlSerializer s;
    
        public AObjectCollection()
        {
            ns= new System.Xml.Serialization.XmlSerializerNamespaces();
            ns.Add( "", "");
            s= new System.Xml.Serialization.XmlSerializer(this.GetType());
            Items = new List<T>();
        }
    
        public List<T> Items { get; set; }
        public DateTime LastSaved { get;set; }
    
        public void Add(T item)
        {
            Items.Add(item);
        }
    
        public void Save(string filePath)
        {
            LastSaved= System.DateTime.Now;
            var xmlws = new System.Xml.XmlWriterSettings { OmitXmlDeclaration = true, Indent= true };
    
            using ( var writer = System.Xml.XmlWriter.Create(filePath, xmlws))
            {
                s.Serialize(writer, this, ns);
            }
        }
    
        public static AObjectCollection<T2> Load<T2>(string filepath)
        {
            AObjectCollection<T2> obj;
            try
            {
                var s= new System.Xml.Serialization.XmlSerializer(typeof(AObjectCollection<T2>));
                using(System.IO.StreamReader reader= System.IO.File.OpenText(filepath))
                {
                    obj= (AObjectCollection<T2>) s.Deserialize(reader);
                }
            }
            catch
            {
                obj= new AObjectCollection<T2>();
            }
    
            return obj;
        }
    }
    

    produces this output:

    <Collection>
      <Items>
        <Item>
          <ID>00000000-0000-0000-0000-000000000000</ID>
          <Name>Test</Name>
        </Item>
      </Items>
      <LastSaved>2010-02-04T07:32:05.812-05:00</LastSaved>
    </Collection>
    

    There are ways to tweak the XML to remove the Collection/Items layer. Search SO for other Collection / Serialization questions.

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

Sidebar

Related Questions

I have the following Model: public class Person { public string Name {get;set;} public
I have the following domain model: public class Name { private readonly string fullName;
I have the following model: public class Person { public string Name { get;
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
I have following model: public class FormularModel { [Required] public string Position { get;
I have the following model public class UserViewModel { public String CVR_Nummer { get;
I have the following model: public class A { public int? Id {get;set;} public
I have the following model: [DataContract] public class MessageHeader { private Guid? messageId; public
ok say i have the following model: public class bar{ public string bar {get;
I have the following model: public class Contact { public Contact() { Name =

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.