0

I'm trying to expand an XML config file containing colors for various things, currently it's looking like this.

<Colors>
    <FooColor1>0x0BD000</FooColor1>
    <FooColor2>0x11C711</FooColor2>
    <FooColor3>0x224466</FooColor3>
    <FooColor4>0xAA3333</FooColor4>
    <FooColor5>0x886644</FooColor5>
</Colors>

These all come out as strings and everything works fine and dandy. The problem comes in here, As I said, I'm trying to expand the file, the new format will be like this

<Colors>
    <DetailColors>
        <FooColor1>0x0BD000</FooColor1>
    <FooColor2>0x11C711</FooColor2>
    <FooColor3>0x224466</FooColor3>
    <FooColor4>0xAA3333</FooColor4>
    <FooColor5>0x886644</FooColor5>
    </DetailColors>
    <GoalColors>
        //Similar stuff, not actually in yet.
    </GoalColors>
</Colors>

However, the serialization style that worked with a single level, doesn't seem to be able to handle the nesting.

[System.SerializableAttribute()]
public class GraphColors
{
  public string FooColor1 { get; set; }
  public string FooColor2 { get; set; }
  public string FooColor3 { get; set; }
  public string FooColor4 { get; set; }
  public string FooColor5 { get; set; }

}
[System.SerializableAttribute()]
public class DetailColors
{
  [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
  public GraphColors ColorSchema { get; set; }
}
[System.SerializableAttribute()]
public class Colors
{
  [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
  private DetailColors CombinedColors { get; set; }
  public static GraphColors getColorsFromConfig()
  {
     return new XmlSerializer(Colors).Deserialize(path).CombinedColors.ColorSchema
  }
}

This worked fine when there wasn't an intermediate object, but now that intermediate object gives a null value. Any help would be greatly appreciated.

3 Answers 3

1

Your c# code seems to be wrong.

Try instead :

public class GraphColors { ... }

public class Colors
{
    public GraphColors DetailColors { get; set; }
}

Your code seems to expect that the xml is made of a Colors element, then a CombinedColors element, then a DetailColors which also contains a ColorSchema.

With your code, this xml could be valid :

<Colors>
  <CombinedColors>
    <ColorSchema>
      <FooColor1>...</FooColor1>
      <FooColor2>...</FooColor2>
      <FooColor3>...</FooColor3>
      <FooColor4>...</FooColor4>
      <FooColor5>...</FooColor5>
    </ColorSchema>
  </CombinedColors>
</Colors>

I think you misunderstood the role of the class name and the property name in xml serialization (and the [Serializable] attribute is not necessary in the case of xml serialization).

When you serialize a class it's the property name which is used to create the xml element name. The type name is not used (only when the type is the xml root element).

Do not add such intermediate class which only add noisy xml elements.

You could also quickly discover the serialized xml by creating a small app which serialize a sample object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't notice I'd put in an extra layer by accident, but more importantly, I didn't realize that it was the name, rather than the class, that was important to have match the xml. Thanks so much.
0

Use a full property implementation: Something like this

private string _Name = "default value";
    public string Name
    {
        get { return _Name; }
        set { _Name=value;  }
    }

Or try setting its default value using the object's constructor.

class YourClass
{
    public YourClass
    {
        //field default value goes here.
    }
}

That will ease the null value.

Hope this helps.

1 Comment

That just feeds me the default values, rather than the values from the config file. I already had some code further down(or is it up?) that applied default values in case a section didn't exist, so this doesn't really help much.
0

This is what my xml->json->csharp says

public class Detailcolors
{
    public int foocolor1 { get; set; }
    public int foocolor2 { get; set; }
    public int foocolor3 { get; set; }
    public int foocolor4 { get; set; }
    public int foocolor5 { get; set; }
}

public class Colors
{
    public Detailcolors detailcolors { get; set; }
    public string goalcolors { get; set; }
}

public class RootObject
{
    public Colors colors { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.