简体   繁体   中英

C# Xml Deserialize nested objects

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.

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.

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.

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM