2011-02-11 21 views
13

XML aracılığıyla serileştirme problemim var çünkü 2 clases bir ilişki kullanıyor (farklı sınıflar!). Ben XML özelliğini kullanarak başka isimle sınıfların 1 dekorasyon denedi ama yine de bana şu hatayı veriyor: İşte XML Serileştirme hatası: 2 tür her ikisi de XML alan adı olan 'İlişki', adından 'kullan'

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

benim 2 sınıfları vardır, kimse neden biliyor ?? Yanlış özniteliği kullanıyorum? Böyle bir XmlRoot ederek iki sınıf süsleyin

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

cevap

13

:-) bunu görmezlikten geldiği görülmekte:

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

Bu iki ilişki sınıflar için iki farklı FQDN'YI üretecek:

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

Teşekkür ederim steve, şimdi çalışıyor – Martin

+3

Belki de soru gider ve onun küçük kapsamı, XMLType (AnonymousType = true) kullanarak "public class Relationship" – hB0

1

Ayrıca alanları da dekore etmelisiniz, örneğin:

[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] sadece belgenin kök öğesi için kullanılır. Diğer türlerde [XmlType] kullanmak istiyorsunuz.

Ayrıca, [Serializable]'a da ihtiyacınız yoktur. XML seri hale getirici bunu yok sayar.

+1

XmlType sorunu giderildi. Teşekkürler! –

0

Tek bir uygulamada harcadığım iki üçüncü taraf web hizmeti ile bu sorunu yaşadım. Garip bir şekilde, dinamik çalışma zamanı jenerasyonu iyiydi (2 dakika sürse de), ama sgen.exe üzüldü.

çözüm

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

Sonra derlemek için csc.exe kullanmak ... svcutil.exe kullanmaktı.

İlgili konular