c# - C# Xml 序列化问题

标签 c# serialization xml-serialization xmlserializer

我有 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LabelTypesCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance="xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LabelTypes>    
    <LabelType>
      <Name>LabelTypeProduct</Name>
    </LabelType>
    <LabelType>
      <Name>LabelTypeClient</Name>
    </LabelType>    
  </LabelTypes>
</LabelTypesCollection>

和 2 个 C# 类:

[Serializable]
[XmlRoot("LabelTypesCollection")]
public class LabelTypesCollection
{
    private static string _labelTypesCollectionPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.Combine(Program.ProgramName, "LabelTypesCollection.xml"));

    [XmlArray("LabelTypes", ElementName="LabelType")]
    public List<LabelType> LabelTypes { get; set; }

    public static LabelTypesCollection LoadAllLabelTypes()
    {
        FileInfo fi = new FileInfo(_labelTypesCollectionPath);
        if (!fi.Exists)
        {
            Logger.WriteLog("Could not find size_types_collection.xml file.", new Exception("Could not find size_types_collection.xml file."));
            return new LabelTypesCollection();
        }

        try
        {
            using (FileStream fs = fi.OpenRead())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(LabelTypesCollection));
                LabelTypesCollection labelTypesCollection = (LabelTypesCollection)serializer.Deserialize(fs);
                return labelTypesCollection;
            }
        }
        catch (Exception ex)
        {
            Logger.WriteLog("Error during loading LabelTypesCollection", ex);
            return null;
        }
    }
}


[Serializable]    
public class LabelType
{
    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlIgnore]
    public string TranslatedName
    {
        get
        {
            string translated = Common.Resources.GetValue(Name);
            return (translated == null) ? Name : translated;
        }
    }
}

当我打电话时:

LabelTypesCollection.LoadAllLabelTypes();

我得到 LabelTypes 列表为空的 LabelTypeCollection 对象。没有错误或任何东西。谁能指出问题所在?

最佳答案

改变这个

[XmlArray("LabelTypes", ElementName="LabelType")]

对此

[XmlArray]

XmlArrayAttributeElementName 指定容器 的元素名称,实际上就是您在构造函数的第一个参数中指定的内容!所以你说的 ctor “这个类序列化为一个名为 LabelTypes 的容器;不用等,实际上我希望容器被命名为 LabelType”。命名参数将覆盖第一个未命名参数的内容。

事实上,由于您希望将容器元素命名为 LabelTypes,这就是成员的实际名称,所以您根本不需要指定它。

您可能一直在考虑 XmlArrayItemAttribute,它控制序列化集合的各个成员的命名 - 但您在这里也不需要它。

我处理 xml 序列化程序的常用方法是手动构建对象,然后查看它们序列化的 xml。在这种情况下,使用您当前拥有的代码生成如下 xml:

<?xml version="1.0" encoding="utf-16"?>
<LabelTypesCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LabelType>
    <LabelType>
      <Name>one</Name>
    </LabelType>
    <LabelType>
      <Name>two</Name>
    </LabelType>
  </LabelType>
</LabelTypesCollection>

这就是让我想到不正确的 LabelType 说明符的原因。

请注意,您也不需要 LabelTypesCollection 上的 XmlRootName 上的 XmlElement,因为您只是指定了 xml 序列化程序无论如何都会提出的内容。

关于c# - C# Xml 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4365816/

相关文章:

具有最小样板的 C++ 类 "upgrade"

ios - swift 3 json序列化

c# - 无法使用 silverlight 类库加载 system.xml

c# - 什么会阻止一个类被序列化?

c# - 如何使用 C# XML 序列化序列化 xml 数组和类属性

c# - 将图标转换为 Png

c# - 在 C# 中使用 ref 参数在堆栈上会发生什么?

java - Google Protobuf 反序列化重复对象上 get 操作的时间复杂度

c# - 输出仅取决于输入和对象状态的方法的名称是什么?

c# - 替换/删除代码或 SQL 中的文本