【问题标题】:XML Serialization error: 2 types both use the XML type name, 'Relationship', from namespace ''XML 序列化错误:2 种类型都使用来自命名空间“”的 XML 类型名称“关系”
【发布时间】:2017-11-22 04:40:13
【问题描述】:

我在通过 XML 进行序列化时遇到问题,因为 2 个类使用一个名为关系的类(尽管是不同的类!)。我尝试使用 XML 属性用另一个名称装饰 1 个类,但它仍然给我以下错误:

{"类型 'SiteServer.Relationship' 和 'LocalServer.Relationship' 都使用来自命名空间 '' 的 XML 类型名称 'Relationship'。使用 XML 属性为该类型指定唯一的 XML 名称和/或命名空间。 "}

这是我的 2 节课,有人知道为什么吗?我使用了错误的属性吗?它似乎忽略了它:-)

public class SiteServer
{
    [XmlRoot("SiteServerRelationShip")]
    public class Relationship
    {
        public string type { get; set; }
    }

    public string Name { get; set; }

    public Relationship Relate = new Relationship();
}

public class LocalServer
{
    public class Relationship
    {
        public string type { get; set; }
    }

    public string Name { get; set; }

    public Relationship Relate = new Relationship();
}

【问题讨论】:

    标签: c# xml exception-handling xml-serialization


    【解决方案1】:

    用这样的 XmlRoot 装饰你的两个类:

    [XmlRoot("SiteServer", Namespace="http://example.com/schemas/SiteServer")]
    public class SiteServer
    {        
        [XmlRoot("SiteServerRelationShip", Namespace="http://example.com/schemas/SiteServer")]
        public class Relationship
        {
            public string type { get; set; }
        }
    
        public string Name { get; set; }
    
        public Relationship Relate = new Relationship();
    }
    
    [XmlRoot("LocalServer", Namespace="http://example.com/schemas/LocalServer")]
    public class LocalServer
    {
        [XmlRoot("LocalServerRelationship", Namespace="http://example.com/schemas/LocalServer")]
        public class Relationship
        {
            public string type { get; set; }
    
        }
    
        public string Name { get; set; }
    
        public Relationship Relate = new Relationship();
    }
    

    这将为两个 RelationShip 类生成两个不同的 FQDN:

    {http://example.com/schemas/LocalServer}LocalServerRelationShip
    {http://example.com/schemas/SiteServer}SiteServerRelationShip
    

    【讨论】:

    • 也许随着问题的发展和它的小范围,在“公共类关系”之上使用 XMLType(AnonymousType=true) 很简单
    【解决方案2】:

    [XmlRoot] 仅用于文档的根元素。您想在其他类型上使用[XmlType]

    另外,您不需要[Serializable]。 XML 序列化程序会忽略它。

    【讨论】:

    • XmlType 解决了我的问题。谢谢!
    【解决方案3】:

    您还必须装饰字段,例如:

    [XmlInclude(typeof(Relationship))]
    public class SiteServer
    {
        [XmlRoot("SiteServerRelationship", Namespace = "http://example.com/schemas/SiteServerRelationship")] 
        public class Relationship
        {
            public string type { get; set; }
        }
    
        public string Name { get; set; }
    
        [XmlElement("SiteServerRelationship", Namespace="http://example.com/schemas/SiteServerRelationship")]       
        public Relationship Relate = new Relationship();
    }
    
    
    [XmlInclude(typeof(Relationship))]    
    public class LocalServer
    {
        [XmlRoot("LocalServerRelationship", Namespace = "http://example.com/schemas/LocalServerRelationship")] 
        public class Relationship
        {
            public string type { get; set; }
        }
    
        public string Name { get; set; }
    
        [XmlElement("LocalServerRelationship", Namespace="http://example.com/schemas/LocalServerRelationship")] 
        public Relationship Relate = new Relationship();
    }
    

    【讨论】:

      【解决方案4】:

      我在一个应用程序中使用了两个第 3 方 Web 服务时遇到了这个问题。奇怪的是,动态运行时生成很好(虽然花了 2 分钟),但 sgen.exe 却不爽。

      解决方案是使用 svcutil.exe...

      svcutil.exe /t:xmlSerializer targetAssemblyOrExecutable  /out:targetAssemblyOrExecutable.XmlSerializers.dll.cs
      

      然后使用 csc.exe 进行编译。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 2022-12-08
        • 1970-01-01
        相关资源
        最近更新 更多