gpt4 book ai didi

c# - "Both use the XML type name X, use XML attributes to specify a unique XML name and/or namespace for the type"怎么解决?

转载 作者:可可西里 更新时间:2023-11-01 08:42:53 26 4
gpt4 key购买 nike

我有以下枚举定义...

namespace ItemTable
{
public enum DisplayMode
{
Tiles,
Default
}
}

namespace EffectiveItemPermissionTable
{
public enum DisplayMode
{
Tree,
FullPaths
}
}

...然后我有以下类(class)...

public class Table<TDisplayMode>
where TDisplayMode: struct
{
// public
public TDisplayMode DisplayMode
{
get { return mDisplayMode; }
set { mDisplayMode = value; }
}

// private
private TDisplayMode mDisplayMode;
}

public class ItemTable : Table<ItemTable.DisplayMode>
{}

public class EffectiveItemPermissionTable : Table<EffectiveItemPermissionTable.DisplayMode>
{}

public class UISettings
{
public UISettings()
{
ItemTable = new ItemTable();
EffectiveItemPermissionTable = new EffectiveItemPermissionTable();
}

public ItemTable ItemTable { get; set; }
public EffectiveItemPermissionTable EffectiveItemPermissionTable { get; set; }
}

...当我尝试使用...序列化 UISettings 的实例时

System.Xml.Serialization.XmlSerializer lSerializer =
new System.Xml.Serialization.XmlSerializer(typeof(UISettings));

...我收到以下错误:

Types 'UISettings.Table`1[EffectiveItemPermissionTable.DisplayMode]' and
'UISettings.Table`1[ItemTable.DisplayMode]' both use the XML type name,
'TableOfDisplayMode', from namespace ''.

Use XML attributes to specify a unique XML name and/or namespace for the type.

我已经尝试使用 XmlType 属性和网上发布的各种解决方案,但没有任何效果。如错误中所述,XML 类型名称始终是 TableOfDisplayMode

目前唯一的解决方案是重命名其中一个枚举,例如到 DisplayMode_,但我觉得那很丑。

最佳答案

您需要提供 Namespace通过在 UISettings 类的属性上使用 XmlElement 属性:

public class UISettings
{
public UISettings()
{

ItemTable = new ItemTable();
EffectiveItemPermissionTable = new EffectiveItemPermissionTable();
}
[XmlElement(Namespace = "Item")]
public ItemTable ItemTable { get; set; }
[XmlElement(Namespace = "Permissions")]
public EffectiveItemPermissionTable EffectiveItemPermissionTable { get; set; }
}

在这里应用时,这将是您的序列化输出:

<?xml version="1.0" encoding="utf-16"?>
<UISettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemTable xmlns="Item">
<DisplayMode>Tiles</DisplayMode>
</ItemTable>
<EffectiveItemPermissionTable xmlns="Permissions">
<DisplayMode>FullPaths</DisplayMode>
</EffectiveItemPermissionTable>
</UISettings>

或者,您可以为类型提供 namespace ,这可能更简洁:

[XmlType(Namespace="Item")]
public class ItemTable : Table<ItemTableNS.DisplayMode>
{ }

[XmlType(Namespace = "Permission")]
public class EffectiveItemPermissionTable : Table<EffectiveItemPermissionTableNS.DisplayMode>
{ }

这将序列化为:

<?xml version="1.0" encoding="utf-16"?>
<UISettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ItemTable>
<DisplayMode xmlns="Item">Tiles</DisplayMode>
</ItemTable>
<EffectiveItemPermissionTable>
<DisplayMode xmlns="Permission">FullPaths</DisplayMode>
</EffectiveItemPermissionTable>
</UISettings>

关于c# - "Both use the XML type name X, use XML attributes to specify a unique XML name and/or namespace for the type"怎么解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38762480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com