Skip to main content
 首页 » 编程设计

c#之C# Xml 序列化问题

2024年02月23日15del

我有 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] 

XmlArrayAttribute 的ElementName 指定容器 的元素名称,实际上就是您在构造函数的第一个参数中指定的内容!所以你说的 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 上的 XmlRoot 或 Name 上的 XmlElement,因为您只是指定了 xml 序列化程序无论如何都会提出的内容。