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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:56:48+00:00 2026-05-13T14:56:48+00:00

I am trying to figure out how to do XML serialization. This is how

  • 0

I am trying to figure out how to do XML serialization.

This is how I want my XML document too look like in the end

<course>
    <name></name>
    <backgroundColor></backgroundColor>
    <fontColor></fontColor>
    <sharingKey></sharingKey>
    <task id="">
        <taskName></taskName>
        <description></description>
    </task>
    <task id="">
        <taskName></taskName>
        <description></description>
    </task>
</course>

So far mine looks like

<?xml version="1.0" encoding="utf-8"?>
<Course xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <name>name</name>
  <backgroundColor>test</backgroundColor>
  <fontColor>test2</fontColor>
  <sharingKey>9324bfab-6cc7-49e5-84f7-56130b8dc099</sharingKey>
  <task id="first Task" />
  <task id="Second task" />
</Course>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot("Course")]
    public class MyWrapper 
    {
        public MyWrapper()
        {
            TaskList = new List<Tasks>();
        }

        [XmlElement("name")]
        public string Name { get; set; }

        [XmlElement("backgroundColor")]
        public string BackgroundColor { get; set; }

        [XmlElement("fontColor")]
        public string  FontColor { get; set; }

        [XmlElement("sharingKey")]
        public Guid SharingKey { get; set; }

        [XmlElement("task")]
        public List<Tasks> TaskList { get; set; }

    }


    public class Tasks
    {
        [XmlAttribute("id")]
        public string Id { get; set; }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Tasks task = new Tasks();
            task.Id = "first Task";

            Tasks task2 = new Tasks();
            task2.Id = "Second task";

            MyWrapper wrap = new MyWrapper();
            wrap.BackgroundColor = "test";
            wrap.FontColor = "test2";
            wrap.Name = "name";
            wrap.SharingKey = Guid.NewGuid();

            wrap.TaskList.Add(task);
            wrap.TaskList.Add(task2);

            SerializeToXML(wrap);
        }

        static public void SerializeToXML(MyWrapper list)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));


       TextWriter textWriter = new StreamWriter(@"C:\New folder\test.xml");
        serializer.Serialize(textWriter, list);
        textWriter.Close();
    }
}

}

So my question is with the “id” for the task.

All I have is another class with this in it

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

How did it know to put this attribute in the task tag?

What happens if I wanted to have another property

[XmlElement()]
public string TaskName {get; set;}

Say I wanted to have an attribute with this element how would I make sure that the attribute would be with TaskName not with Task?

  • 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-13T14:56:49+00:00Added an answer on May 13, 2026 at 2:56 pm
    [XmlElement("task")]
    public List<Tasks> TaskList { get; set; }
    

    This part of your code told it to serialize every Tasks object in an element called “task”. The serializer looks at the properties on the Tasks class and finds the Id property which you marked with [XmlAttribute("id")] so it gets serialized as an attribute to the “task” element for the object.

    I’m not sure I understand your second question. You cannot add an attribute to the element because its type is a string. Instead you would have to create a new class to wrap the concept of a task name which would have a name property and whatever other properties you wanted to add to it.

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

Sidebar

Related Questions

I'm trying to figure out serialization of .net arrays to XML. Here's a piece
I am trying to figure out the best way to generate an XML sitemap
I am trying to figure out the best way to handle a scenario where
I'm trying to figure out how to structure data properly in PHP in order
I am trying to figure out how I can load my table variable with
I've been trying to figure out how to get a basic HTTP Basic auth
I'm trying to figure out how to use a preference (the ones you can
Hi guys i am trying to figure out a simple regEx from the given
I hope I asked that correctly. I am trying to figure out what element.sourceline
I'm porting a program I wrote on C# to Cocoa and I'm trying to

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.