2011-02-11 14 views
13

Tengo problemas para serializar a través de XML porque 2 clases usan una clase (¡aunque diferentes clases!) Llamada Relación. He tratado de decorar 1 de las clases con otro nombre usando el atributo XML pero todavía me da el siguiente error:Error de serialización XML: 2 tipos usan el nombre de tipo XML, 'Relación', desde el espacio de nombres ''

{"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."}

Éstos son mis 2 clases, alguien sabe por qué ?? ¿Estoy usando el atributo equivocado? Parece estar haciendo caso omiso de él :-)

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

Respuesta

13

decorar sus dos clases por un XmlRoot así:

[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(); 
} 

Esto producirá dos diferentes FQDN para las clases de dos Relación:

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

gracias steve, ahora está trabajando – Martin

+3

Tal vez como dice la pregunta y su pequeño alcance, es simple usar XMLType (AnonymousType = true) en la parte superior de "relación de clase pública" – hB0

1

También debe decorar los campos, p. Ej .:

[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(); 
} 
6

[XmlRoot] solo se usa para el elemento raíz del documento. Desea utilizar [XmlType] en otros tipos.

Además, no necesita [Serializable]. El serializador XML lo ignora.

+1

XmlType solucionó mi problema. ¡Gracias! –

0

Tuve este problema con dos webservices de terceros que consumía en una aplicación. Curiosamente, la generación de tiempo de ejecución dinámico estaba bien (aunque tardó 2 minutos), pero sgen.exe se molestó.

La solución fue utilizar svcutil.exe ...

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

A continuación, utilice csc.exe para compilarlo.

Cuestiones relacionadas