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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:03:22+00:00 2026-05-21T18:03:22+00:00

public class Hat { [XmlTextAttribute] public string Name { get; set; } [XmlAttribute(Color)] public

  • 0
public class Hat
{
    [XmlTextAttribute]
    public string Name { get; set; }
    [XmlAttribute("Color")]
    public string Color { get; set; }
}

var hat1 = new Hat {Name="Cool Hat", Color="Red"};
var hat2 = new Hat {Name="Funky Hat", Color=null};

This is what I get (notice missing color-attribute on Funky Hat):

<Hats>
 <Hat Color="Red">Cool Hat</Hat>
 <Hat>Funky Hat</Hat>
</Hats>

This is what I want:

<Hats>
 <Hat Color="Red">Cool Hat</Hat>
 <Hat Color="">Funky Hat</Hat>
</Hats>

How can I force the serializer to create an empty attribute in stead of leaving it out?

EDIT:

Turns out I am an idiot and created an example that contained an error, because I wanted to simplify the code for the example.

If the value of color is “” (or string.empty), it is actually serialized as an empty attribute. However, I really had a null value, not an empty string – hence it was left out.

So the behavior I wanted was actually already the behavior of the example I created.

Sorry, guys!

  • 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-21T18:03:22+00:00Added an answer on May 21, 2026 at 6:03 pm

    Try using List<Hat> as the container. Using this:

    var hats = new List<Hat>
        {
            new Hat { Name = "Cool Hat", Color = "Red" }, 
            new Hat { Name = "Funky Hat", Color = string.Empty }
        };
    
    using (var stream = new FileStream("test.txt", FileMode.Truncate))
    {
        var serializer = new XmlSerializer(typeof(List<Hat>));
        serializer.Serialize(stream, hats);
    }
    

    I get this:

    <?xml version="1.0"?>
    <ArrayOfHat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Hat Color="Red">Cool Hat</Hat>
      <Hat Color="">Funky Hat</Hat>
    </ArrayOfHat>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Emp { public int ID { get; set; } public string Name
public class Address { public string ZipCode {get; set;} } public class Customer {
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class CovariantTest { public A getObj() { return new A(); } public static
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
In the following snippet: public class a { public void otherMethod(){} public void doStuff(String
public class Main { public static void main(String[] args) throws InterruptedException { ClassA a
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class Item { ... } public class Order { public List<Item> Items ...

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.