views:

9

answers:

1

Hi

I have the following types

[Serializable, XmlType(Namespace="http://mycompany/foo"]
public sealed class Limit
{
    [XmlElement(ElementName="Value1")]
    public double Value1 {get;set;}

    [XmlElement(ElementName="ComplexValue1")]
    public ComplexValue ComplexValue1 {get;set;}
}

[Serializable, XmlType(Namespace="http://mycompany/foo"]
public sealed class ComplexValue 
{
    [XmlElement(ElementName="Item1")]
    public double Item1 {get;set;}

    [XmlElement(ElementName="Item2")]
    public double Item2 {get;set;}
}

which I want to serialize to a .settings file.

When I copy the blob below into the settings file, I lose the ComplexValue1 element somehow:

<?xml version="1.0" encoding="utf-16"?>
<Limit>
  <Value1>20</Value1>
  <ComplexValue1>
     <Item1>2.0</Item1>
     <Item2>5.0</Item2>
  </ComplexValue1>
</Limit>

i.e. Visual Studio transforms it to:

<?xml version="1.0" encoding="utf-16"?>
<Limit>
<Value1>20</Value1>
</Limit>

with a bunch of namespaces that I think don't matter for the question...

What am I missing?

+1  A: 
  1. You don't need the Serializable attribute for XML serialization

  2. I guess you should remove the XmlType attribute to solve the issue.

  3. You specify a namespace but there are none in the XML file? This should fit too.

  4. Use XmlRoot for the root node if you like

Scoregraphic
Those points do not solve the issue, indeed, the problem is the habit of VS injecting a "default" value inside the Settings.designer.cs file. But thanks for the recommendations anyway...
Román