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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:25:15+00:00 2026-06-01T00:25:15+00:00

I want to write a xml file with C#. It is a basic file

  • 0

I want to write a xml file with C#. It is a basic file like that :

<EmployeeConfiguration>
  <Bosses>
    <Boss name="BOB">
      <Employees>
        <Employee Id="#0001" />
        <Employee Id="#0002" />
      </Employees>
    <Boss>
  </Bosses>
</EmployeeConfiguration>

and I don’t want have an Employees node if there is not Employee node…

I want use XElement but I can’t because of that… So I used XmlWriter. it works fine but I find it’s very verbose to write XML :

EmployeeConfiguration config = EmployeeConfiguration.GetConfiguration();

using (XmlWriter writer = XmlWriter.Create(_fileUri, settings))
{
  writer.WriteStartDocument();

  // <EmployeeConfiguration>
  writer.WriteStartElement("EmployeeConfiguration");

  if (config.Bosses.Count > 0)
  {
    // <Bosses>
    writer.WriteStartElement("Bosses");

    foreach (Boss b in config.Bosses)
    {
      // Boss
      writer.WriteStartElement("Boss");
      writer.WriteStartAttribute("name");
      writer.WriteString(b.Name);
      writer.WriteEndAttribute();

      if (b.Employees.Count > 0)
      {
        writer.WriteStartElement("Employees");

        foreach (Employee emp in b.Employees)
        {
            writer.WriteStartElement(Employee);
            writer.WriteStartAttribute(Id);
            writer.WriteString(emp.Id);
            writer.WriteEndAttribute();
            writer.WriteEndElement();
        }
      }
    }
  }
}

Is there another (and fastest) way to write this kind of xml file ?

  • 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-01T00:25:17+00:00Added an answer on June 1, 2026 at 12:25 am

    If you mean “fastest” as the fastest way to write some code to do it (and the simplest), then creating a custom class and serializing it using the XmlSerializer is the way to go…

    Create your classes as follows:

    [XmlRoot("EmployeeConfiguration")]
    public class EmployeeConfiguration
    {
        [XmlArray("Bosses")]
        [XmlArrayItem("Boss")]
        public List<Boss> Bosses { get; set; }
    }
    
    public class Boss
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
    
        [XmlArray("Employees")]
        [XmlArrayItem("Employee")]
        public List<Employee> Employees { get; set; }
    }
    
    public class Employee
    {
        [XmlAttribute]
        public string Id { get; set; }
    }
    

    and then you can serialize these out with this:

    // create a serializer for the root type above
    var serializer = new XmlSerializer(typeof (EmployeeConfiguration));
    
    // by default, the serializer will write out the "xsi" and "xsd" namespaces to any output.
    // you don't want these, so this will inhibit it.
    var namespaces = new XmlSerializerNamespaces(new [] { new XmlQualifiedName("", "") });
    
    // serialize to stream or writer
    serializer.Serialize(outputStreamOrWriter, config, namespaces);
    

    As you can see – using various attributes on the classes instruct the serializer in how it should serialize the class. Some of the ones I’ve included above are actually the default settings and don’t explicitly need to be stated – but I’ve included them to show you how it is done.

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

Sidebar

Related Questions

I want to write a XML file as below: <?xml version=1.0 encoding=UTF-8?> <books xmlns:xsd=http://www.w3.org/2001/XMLSchema
I want to write a .xml file using the following code into the App_Data/posts.
I want to write the bulk of data into xml file ,the data coming
I need to generate an XML file in C#. I want to write the
I want to read an xml file, apply a transform, then write to another
I want to write an XML file. I have created an XSD file named
I want to write a java code to read the input XML file and
I want to write an xslt to transform one xml file to another. The
lets say i have an xml file path.xml that goes like this: <paths> <path
I want to write in an xml file located under the conf directory of

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.