【问题标题】:Windows Universal XML SerializationWindows 通用 XML 序列化
【发布时间】:2016-02-28 01:31:57
【问题描述】:

我一直在开发一款厨房应用,它需要保存食谱。除了List<IngredientMeasure> IngredientList,XML 序列化一切正常。我听说很多其他人在序列化类列表时也遇到了问题,我看到了这个:Serializing Lists of Classes to XML。 我在那里尝试了解决方案,但没有一个有效。 以下是 Recipe 类的字段:

[DataContract]
public class Recipe
{
    [DataMember]
    public static List<Recipe> RecipeList = new List<Recipe>();

    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string FilePath { get;  set; }

   [DataMember]
    public List<IngredientMeasure> IngredientList { get;  set; }

    [XmlArray("IngredientsList")]
    public List<string> Ingredients = new List<string>();

    [DataMember]
    public List<string> DirectionList { get; private set; }
    [DataMember]
    public List<string> Notes { get; private set; }

    public Recipe()
    {

    }
}

这是IngredientMeasure 类:

public class IngredientMeasure
{
    [DataMember]
    public Ingredient ingredient;
    [DataMember]
    public Measure measure;

    public IngredientMeasure()
    {

    }
}

感谢任何帮助。

【问题讨论】:

  • 请问您能粘贴异常吗?
  • 欢迎使用 StackOverflow,能否请您粘贴您要生成的 xml 文本?

标签: c# xml windows serialization win-universal-app


【解决方案1】:

从您刚才提到的链接中,XmlArray 应该在包含的项目列表中而不是在字符串列表中。

[DataContract]
public class Recipe
{

    [XmlArray("IngredientsList", XmlArrayItem(typeof(IngredientMeasure), ElementName = "IngredientMeasure"))]
    public List<IngredientMeasure> IngredientList { get;  set; }

    // ... other properties 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2014-09-08
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    相关资源
    最近更新 更多