0
votes

I have an existing class that is marked with [Serializable] attribute and has some primitive as well as user defined objects as properties that get serialized to the xml file.Everything is working fine until now. Here is how my class definition looks like

    [Serializable]
public class MyPlan
{
    [XmlElement]
    public string PlanFolder { get; set; }

    private string _name;
   [XmlElement]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private SystemText _SystemText;
    [XmlElement]
    public SystemText SystemText
    {
        get
        {
            if (ReferenceEquals(_SystemText, null))
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.CoordinateSystemTextRequired));
            return _SystemText;
        }
        set
        {
            if (ReferenceEquals(value, null))
                throw new ArgumentNullException("value");
            _SystemText = value;
        }
    }

    private ObservableCollection<PlanData> _planData;
    [XmlArrayItem("PlanData")]
    public ObservableCollection<WellPad> PlanData
    {
        get { return _planData ?? (_planData = new ObservableCollection<PlanData>()); }
    }

    internal void Write(string plnDirectory)
    {
        var planDirectory = Path.Combine(plnDirectory, Key);
        if (!Directory.Exists(planDirectory))
            Directory.CreateDirectory(planDirectory);

        ObjectSerializer.Write(Path.Combine(planDirectory, fileName), this);
    }

    internal static PlanData Read(string name, string projectDirectory, string SystemFolder)
    {
        var planDirectory = Path.Combine(projectDirectory, SystemFolder, name);
        var path = Path.Combine(planDirectory, FileName);
        return Read(path);
    }

    internal static Plan Read(string path)
    {
        if (!Directory.Exists(Path.GetDirectoryName(path))
            || !File.Exists(path))
            throw new FileNotFoundException();
        return ObjectSerializer.Read<Plan>(path);
    }

    private Defaults _planDefaults
    [XmlElement]
    public Defaults PlanDefaults
    {
        get { return _planDefaults ?? (_planDefaults = new Defaults()); }
        set { _planDefaults = value; }
    }

    public ObservableCollection<Plan> _previousPlans
    [XmlArrayItem("Plan")]
    public ObservableCollection<Plan> previousPlans
    {
        get { return _previousPlans ?? (_previousPlans = new ObservableCollection<Plan>()); }
    }
}

Now I am trying to add a new string property that just has a getter/setter and is public and it has a [xmlElement] attribute as well. For some reason, this property is not getting serialized to the xml file. If I change the type of the attribute to Integer or Double, then it does get serialized. What is the issue here?

1
Does the property have a value when you're trying to serialize the object? It might help if you show a populated sample object as well as the serialized output. - Anthony Pegram
Initially the property does not have value but at some point in my program, I hit a workflow that does set the value and then Write method is called to serialize the xml. But the new property does not appear .. If I change type of property from string to Int or double, then it does appear - WAQ

1 Answers

0
votes

I think this happen due to your field doesn't initialized. Initialize your property to String.Empty and try.