XML序列化错误:2类型都使用XML类型名称,“关系”,从命名空间' [英] XML Serialization error: 2 types both use the XML type name, 'Relationship', from namespace ''
问题描述
我有一个称为关系问题通过XML序列化,因为2 clases使用一个类(虽然不同的类!)。我曾尝试使用XML属性装饰用另一个名字的类1,但它仍然给我下面的错误:
{类型 SiteServer.Relationship'和'LocalServer.Relationship都使用XML类型的名称,关系,从命名空间'。使用XML属性来指定类型的唯一XML名称和/或命名空间。}
块引用>
下面是我的2班,任何人都知道为什么?我使用了错误的属性?这似乎是忽略它: - )
公共类SITESERVER
{
[XmlRoot(SiteServerRelationShip )]
类公共关系
{
公共字符串类型{搞定;组; }
}
公共字符串名称{;组; }
公共关系相关=新的关系();
}
公共类的LocalServer
{
类公共关系
{
公共字符串类型{搞定;组; }
}
公共字符串名称{;组; }
公共关系相关=新的关系();
}
解决方案由一个装饰你的两个班XmlRoot是这样的:
[XmlRoot(SITESERVER,命名空间=http://example.com/schemas/SiteServer )
公共类SITESERVER
{
[XmlRoot(SiteServerRelationShip,命名空间=http://example.com/schemas/SiteServer)]
类公共关系
{
公共字符串类型{搞定;组; }
}
公共字符串名称{;组; }
公共关系相关=新的关系();
}
[XmlRoot(的LocalServer,命名空间=http://example.com/schemas/LocalServer)]
公共类的LocalServer
$ { b $ b〔XmlRoot(LocalServerRelationship,命名空间=http://example.com/schemas/LocalServer)]
类公共关系
{
公共字符串类型{搞定;组; }
}
公共字符串名称{;组; }
公共关系相关=新的关系();
}
这将产生两个不同的FQDN两个关系类:
{} http://example.com/schemas/LocalServer LocalServerRelationShip
{http://example.com/schemas/SiteServer } SiteServerRelationShip
I am having a problem serializing via XML because 2 clases use a class (although different classes!) called Relationship. I have tried decorating 1 of the classes with another name using the XML attribute but it still gives me the following error:
{"Types 'SiteServer.Relationship' and 'LocalServer.Relationship' both use the XML type name, 'Relationship', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type."}
Here are my 2 classes, anyone know why ?? AM i using the wrong attribute? It seems to be ignoring it :-)
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(); }
解决方案Decorate your two classes by an XmlRoot like this :
[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(); }
This will produce two different FQDN for the two RelationShip classes :
{http://example.com/schemas/LocalServer}LocalServerRelationShip {http://example.com/schemas/SiteServer}SiteServerRelationShip
这篇关于XML序列化错误:2类型都使用XML类型名称,“关系”,从命名空间'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!