简体   繁体   English

C#Xml反序列化嵌套对象

[英]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. 我正在尝试扩展一个包含各种颜色的XML配置文件,目前看起来像这样。

<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. 您的C#代码似乎是错误的。

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 . 你的代码似乎想到的是,XML是由的Colors元素,那么CombinedColors元素,那么DetailColors其中也包含了ColorSchema

With your code, this xml could be valid : 使用您的代码,此xml可能有效:

<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). 我认为您误解了XML序列化中类名和属性名的作用(对于xml序列化,[Serializable]属性不是必需的)。

When you serialize a class it's the property name which is used to create the xml element name. 序列化一个类时,它是用于创建xml元素名称的属性名称。 The type name is not used (only when the type is the xml root element). 不使用类型名称(仅当类型是xml根元素时)。

Do not add such intermediate class which only add noisy xml elements. 不要添加仅添加嘈杂的xml元素的中间类。

You could also quickly discover the serialized xml by creating a small app which serialize a sample object. 您还可以通过创建一个序列化示例对象的小应用程序来快速发现序列化的xml。

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 这就是我的xml-> json-> csharp所说的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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