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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:23:05+00:00 2026-06-12T19:23:05+00:00

I have created a couple of classes mean to represent a relational data structure

  • 0

I have created a couple of classes mean to represent a relational data structure ( parent child structures ). Below is an example of XML representation so far giving you an idea of what I mean

<BillingFile>
    <Account>
      <acctnum>122344231414</acctnum>
      <adjustments>34.44</adjustments>
      <Charges>
        <lineitem>
          <chargetype>PENALTY</chargetype>
          <amount>40.50</amount>
          <ratecode>E101</ratecode>
        </lineitem>
        <lineitem>
          <chargetype>LATE CHARGE</chargetype>
          <amount>445.35</amount>
          <ratecode>D101</ratecode>
        </lineitem>
      </Charges>
    </Account>
</BillingFile>

What I’m doing with my application is parsing through a large text file which could have upwards of 50,000+ accounts in it. Each time an account is read, I will create an “Account” object that has the parent objects, etc. The end goal is to be able to create an XML file containing all this account info that is serialized from the objects created.

The problem I see with this, is that if I store all these objects in memory it will cause a performance issue as it runs in those 50k+ record files.

What I’m wondering is, is there a way to sequentially serialize an object in C#, rather than all at once?

I’ve done some googling and it seems that the built in serialization methods of .NET are a one and done kind of deal. Is there a better way I can do this?

I’d rather avoid having to do any intermediate steps like storing the data in a database, since it’s easier to modify code than it is to mess with a bunch of tables and JOIN statements.

Thoughts?

  • 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-12T19:23:07+00:00Added an answer on June 12, 2026 at 7:23 pm

    XmlSerializer.Deserialize takes an XmlReader parameter. You could place the XmlReader just at the <Account> tag, and call the XmlSerializer there.

    public IEnumerable<Account> ReadAccounts(TextReader source)
    {
        var ser = new XmlSerializer(typeof(Account));
    
        using (var reader = XmlReader.Create(source))
        {
            if (!reader.IsStartElement("BillingFile"))
            {
                yield break;
            }
    
            reader.Read();
    
            while (reader.MoveToContent() == XmlNodeType.Element)
            {
                yield return (Account) ser.Deserialize(reader);
            }
        }
    }
    

    Similarly for serialization

    public void WriteAccounts(IEnumerable<Account> data, TextWriter target)
    {
        // Use XmlSerializerNamespaces to supress xmlns:xsi and xmlns:xsd
        var namespaces = new XmlSerializerNamespaces();
        namespaces.Add("", "");
    
        var ser = new XmlSerializer(typeof(Account));
    
        using (var writer = XmlWriter.Create(target))
        {
            writer.WriteStartElement("BillingFile");
    
            foreach (var acct in data)
            {
                ser.Serialize(writer, acct, namespaces);
                writer.Flush();
            }
    
            writer.WriteEndElement();
        }
    }
    

    You could also create a BillingFile class that implements IXmlSerializable, and put this functionality there.

    Or if you prefer a push-based model instead:

    public class AccountWriter : IDisposable
    {
        private XmlWriter _writer;
        private XmlSerializer _ser;
        private XmlSerializerNamespaces _namespaces;
    
        private bool _wroteHeader = false;
        private bool _disposed = false;
    
        public bool IsDisposed { get { return _disposed; } }
    
        public AccountWriter(TextWriter target)
        {
            _namespaces = new XmlSerializerNamespaces();
            _namespaces.Add("", "");
    
            _ser = new XmlSerializer(typeof(Account));
    
            _writer = XmlWriter.Create(target);
        }
    
        public void Write(Account acct)
        {
            if (_disposed) throw new ObjectDisposedException("AccountWriter");
    
            if (!_wroteHeader)
            {
                _writer.WriteStartElement("BillingFile");
                _wroteHeader = true;
            }
    
            _ser.Serialize(_writer, acct, _namespaces);
        }
    
        public void Flush()
        {
            if (_disposed) throw new ObjectDisposedException("AccountWriter");
            _writer.Flush();
        }
    
        public void Dispose()
        {
            if (!_disposed)
            {
                if (_wroteHeader)
                {
                    _writer.WriteEndElement();
                    _wroteHeader = true;
                }
    
                _writer.Dispose();
                _disposed = true;
            }
        }
    }
    
    using (var writer = new AccountWriter(Console.Out))
    {
        foreach (var acct in accounts)
        {
            writer.Write(acct);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have a couple of classes that I have created for my web project
I have created a couple of java working sets for a project in my
I have a couple extension methods that handle serialization of my classes, and since
I have created a couple of PowerShell scripts which configure computers used in a
I have a couple classes that look like this public class ParentClass { public
I have a couple of classes that are singletons so I tried to create
I've created a couple of Comparer classes for FileInfo objects to allow sorting by
Imagine I have a bunch of classes, that can do logging, so I've created
I have a couple of classes: StateProcessor and State . I want to write

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.