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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:01:53+00:00 2026-06-18T01:01:53+00:00

I have next XML file: <Root> <Document> <Id>d639a54f-baca-11e1-8067-001fd09b1dfd</Id> <Balance>-24145</Balance> </Document> <Document> <Id>e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd</Id> <Balance>0.28</Balance> </Document>

  • 0

I have next XML file:

<Root>
    <Document>      
        <Id>d639a54f-baca-11e1-8067-001fd09b1dfd</Id>
        <Balance>-24145</Balance>
    </Document>
    <Document>      
        <Id>e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd</Id>
        <Balance>0.28</Balance> 
    </Document>
</Root>

I deserialize it to this class:

[XmlRoot("Root", IsNullable = false)]
public class DocBalanceCollection
{
    [XmlElement("Document")]
    public List<DocBalanceItem> DocsBalanceItems = new List<DocBalanceItem>();
}

where DocBalanceItem is:

public class DocBalanceItem
{
    [XmlElement("Id")]
    public Guid DocId { get; set; }

    [XmlElement("Balance")]
    public decimal? BalanceAmount { get; set; }
}

Here is my deserialization method:

public DocBalanceCollection DeserializeDocBalances(string filePath)
{
    var docBalanceCollection = new DocBalanceCollection();

    if (File.Exists(filePath))
    {
        var serializer = new XmlSerializer(docBalanceCollection.GetType());
        TextReader reader = new StreamReader(filePath);
        docBalanceCollection = (DocBalanceCollection)serializer.Deserialize(reader);
        reader.Close();
    }

    return docBalanceCollection;
}

All works fine but I have many XML files. Besides writing Item classes I have to write ItemCollection classes for each of them. And also I have to implement DeserializeItems method for each.

Can I deserialize my XML files without creating ItemCollection classes? And can I write single generic method to deserialize all of them?

The only solution that comes to mind – make an interface for all these classes. Any ideas?

  • 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-18T01:01:54+00:00Added an answer on June 18, 2026 at 1:01 am

    You can deserialize a generic List<T> just fine with XmlSerializer. However, first you need to add the XmlType attribute to your DocBalanceItem so it knows how the list elements are named.

    [XmlType("Document")]
    public class DocBalanceItem
    {
        [XmlElement("Id")]
        public Guid DocId { get; set; }
    
        [XmlElement("Balance")]
        public decimal? BalanceAmount { get; set; }
    }
    

    Then modify your DeserializeDocBalances() method to return a List<T> and pass the serializer an XmlRootAttribute instance to instruct it to look for Root as the root element:

    public List<T> DeserializeList<T>(string filePath)
    {
        var itemList = new List<T>();
    
        if (File.Exists(filePath))
        {
            var serializer = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("Root"));
            TextReader reader = new StreamReader(filePath);
            itemList = (List<T>)serializer.Deserialize(reader);
            reader.Close();
        }
    
        return itemList;
    }
    

    Then you should be able to do

    var list = DeserializeList<DocBalanceItem>("somefile.xml");

    Since the method now returns a generic List<T>, you no longer need to create custom collections for every type.

    P.S. – I tested this solution locally with the provided document, it does work.

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

Sidebar

Related Questions

I have the next code to retrieve some data from a xml file. The
I want to ask your advice in next situation. I have xml file with
I'm trying to get only elements that have text, ex xml : <root> <Item>
I have a big (1.9 GB) XML file which has data I want to
I have xml file and that create view and i pass that screen shot
I have an XML file in outside of my project folder, and I want
I have the next directory structure for my personal project: mines/ build.xml build/ build/classes/
I have an xml file called singers.xml . I have singers in it, and
I have an xml file with following structure: <table name=tblcats> <row> <Id>3680</Id> <Industry>Associations</Industry> <ParentId>1810</ParentId>
I have an xml file with following structure: <table name=tblcats> <row> <Id>3680</Id> <Industry>Associations</Industry> <ParentId>1810</ParentId>

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.