Mar*_*tin 14 c# xml exception-handling xml-serialization
我在通过XML序列化时遇到问题,因为2个clases使用一个名为Relationship的类(尽管是不同的类!).我尝试使用XML属性用另一个名称装饰其中一个类,但它仍然给我以下错误:
{"Types'SiteServer.Relationship'和'LocalServer.Relationship'都使用XML类型名称'Relationship',来自命名空间''.使用XML属性为类型指定唯一的XML名称和/或名称空间."}
这是我的两个班,有谁知道为什么?我使用错误的属性?似乎忽略了它:-)
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();
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*e B 13
用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();
}
Run Code Online (Sandbox Code Playgroud)
这将为两个RelationShip类生成两个不同的FQDN:
{http://example.com/schemas/LocalServer}LocalServerRelationShip
{http://example.com/schemas/SiteServer}SiteServerRelationShip
Run Code Online (Sandbox Code Playgroud)