如何解决“两者都使用XML类型名称X,使用XML属性为该类型指定唯一的XML名称和/或名称空间"? [英] How to solve "Both use the XML type name X, use XML attributes to specify a unique XML name and/or namespace for the type"?

查看:537
本文介绍了如何解决“两者都使用XML类型名称X,使用XML属性为该类型指定唯一的XML名称和/或名称空间"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下枚举定义...

I have the following enum definitions...

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

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

...然后我有以下课程...

...and then i have the following classes...

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的实例时

...and when i try to serialize an instance of UISettings with...

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

...我收到以下错误:

...i get the following error:

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.

I have tried to use XmlType attribubtes and all sorts of solutions posted on the web but nothing works. The XML type name is always TableOfDisplayMode as mentioned in the error.

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

The only solution right now is to renamed one of the enums, e.g. to DisplayMode_ but i find that rather ugly.

推荐答案

您需要提供

You need to provide the Namespace by using the XmlElement attribute on the properties of your UISettings class:

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

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

When applied here this will be your serialized output:

<?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>

或者,也许更干净,您可以在以下类型上提供命名空间:

Alternatively, and maybe cleaner, you can provide the Namespace on the types:

[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>

这篇关于如何解决“两者都使用XML类型名称X,使用XML属性为该类型指定唯一的XML名称和/或名称空间"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆