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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:28:42+00:00 2026-06-18T16:28:42+00:00

I need to create an XML file using C#. I am using a class

  • 0

I need to create an XML file using C#.
I am using a class that inherits List that represents a list of computers and later initialize it with values but the serializer doesn’t get the attributes for this class, only for its descendants.
this is the class:

public class Computers : List<Computer>
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }


        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }            

    }

    public class Computer 
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }

        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }

        public string IPAddress { get; set; }
        public string Name { get; set; }
    }

The result should look something like this:

<fpc4:Computers StorageName="Computers" StorageType="1">
    <fpc4:Computer StorageName="{D37291CA-D1A7-4F34-87E4-8D84F1397BEA}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer1</fpc4:Name>
    </fpc4:Computer>
    <fpc4:Computer StorageName="{AFE5707C-EA71-4442-9CA8-2A6264EAA814}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer2</fpc4:Name>
    </fpc4:Computer>

But what I get so far is this:

<fpc4:Computers>
    <fpc4:Computer StorageType="1" StorageName="{7297fc09-3142-4284-b2e9-d6ea2fb1be78}">
      <fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
      <fpc4:Name>Computer1</fpc4:Name>
    </fpc4:Computer>
    <fpc4:Computer StorageType="1" StorageName="{eab517f6-aca9-4d01-a58b-143f2e3211e7}">
      <fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
      <fpc4:Name>Computer2</fpc4:Name>
    </fpc4:Computer>
  </fpc4:Computers>

As you can see the Computers node which is the parent node doesn’t get the attributes.

Do you guys have a solution?

  • 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-18T16:28:43+00:00Added an answer on June 18, 2026 at 4:28 pm

    XmlSerializer treats lists completely separate to leaf nodes; properties on lists do not exist – it is just a collection of the contained data. A better approach would be:

    public class Computers {
        private readonly List<Computer> items = new List<Computer>();
        [XmlElement("Computer")]
        public List<Computer> Items { get { return items; } }
    
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }
    
        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }   
    }
    

    This is an object that has a set of computers and has two attributes – but is not a list itself. The use of XmlElementAttribute for the list flattens the nesting as desired. Note that I have omitted namespaces for convenience.

    Inheriting from a list (with an aim of adding members) will not work well, not just for XmlSerlaizer, but for a wide range of serializers and binding frameworks.

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

Sidebar

Related Questions

I need to create program that creates a XML schema like below using System.Xml.XmlSchema
I need to create an XML schema definition (XSD) that describes Java objects. I
I need to create a web service that returns XML data to some of
I am using a class library which represents some of its configuration in .xml.
I have a class that processes XML files. It works, but the problem is
I have a very large XML file that I need to parse so I
For my application, I need to serialize data to file using XML, so I
I have a List<Item> collection that I am trying to generate an xml file
I need to deserialize xml file to List or Array then enlarge this List
I'm trying to create a custom class that is based off of an XML

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.