.NET, Why must I use *Specified property to force serialization? Is there a way to not do this?

The FooSpecified property is used to control whether the Foo property must be serialized. If you always want to serialize the property, just remove the FooSpecified property.


I know this is an old question, but none of the other answers (except perhaps the suggestion of using Xsd2Code) really produces an ideal solution when you're generating code as part of your build and your .xsd may change several times during a single release cycle.

An easy way for me to get what I really wanted and still use xsd.exe was to run the generated file through a simple post-processor. The code for the post-processor is as follows:

namespace XsdAutoSpecify
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            if (args.Length != 1)
            {
                throw new ArgumentException("Specify a file name");
            }

            string fileName = args[0];
            Regex regex = new Regex(".*private bool (?<fieldName>.*)Specified;");

            IList<string> result = new List<string>();
            IDictionary<string, string> edits = new Dictionary<string, string>();

            foreach (string line in File.ReadLines(fileName))
            {
                result.Add(line);
                if (line.Contains("public partial class"))
                {
                    // Don't pollute other classes which may contain like-named fields
                    edits.Clear();
                }
                else if (regex.IsMatch(line))
                {
                    // We found a "private bool fooSpecified;" line.  Add
                    // an entry to our edit dictionary.
                    string fieldName = regex.Match(line).Groups["fieldName"].Value;
                    string lineToAppend = string.Format("this.{0} = value;", fieldName);
                    string newLine = string.Format("                this.{0}Specified = true;", fieldName);
                    edits[lineToAppend] = newLine;
                }
                else if (edits.ContainsKey(line.Trim()))
                {
                    // Use our edit dictionary to add an autospecifier to the foo setter, as follows:
                    //   set {
                    //       this.fooField = value;
                    //       this.fooFieldSpecified = true;
                    //   }
                    result.Add(edits[line.Trim()]);
                }
            }

            // Overwrite the result
            File.WriteAllLines(fileName, result);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Environment.Exit(-1);
        }
    }
}
}

The result is generated code similar to the following:

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public barEnum foo {
        get {
            return this.fooField;
        }
        set {
            this.fooField = value;
            this.fooFieldSpecified = true;
        }
    }

You could add a default value to your schema and then use the DefaultValueAttribute.

For example, you could have the following in your schema:

<xs:element name="color" type="xs:string" default="red"/>

And then the following property for serialization:

[DefaultValue(red)]
public string color { get; set; }

This should force the color property to always serialize as "red" if it has not been explicitly set to something else.