2011-02-11 13 views
13

से उपयोग करते हैं, मुझे XML के माध्यम से क्रमबद्ध करने में समस्या आ रही है क्योंकि 2 क्लासेस एक वर्ग (हालांकि विभिन्न वर्ग!) का उपयोग करते हैं, जिसे रिलेशनशिप कहा जाता है। मैं सजा XML विशेषता का उपयोग करते हुए एक और नाम के साथ वर्गों में से 1 की कोशिश की है लेकिन यह अभी भी मुझे निम्न त्रुटि देता है:एक्सएमएल सीरियलाइजेशन त्रुटि: 2 प्रकार दोनों एक्सएमएल टाइप नाम, 'रिलेशनशिप', नेमस्पेस '

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

यहाँ मेरी 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(); 
} 

उत्तर

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

यह दो रिश्ता कक्षाओं के लिए दो अलग-अलग FQDN का उत्पादन करेगा:

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

आप स्टीव धन्यवाद, इसकी अब – Martin

+3

काम कर प्रश्न चला जाता है और उसके कम गुंजाइश है, यह "सार्वजनिक वर्ग रिश्ता" – hB0

1

आपको फ़ील्ड को भी सजाने के लिए है, उदाहरण के लिए:

[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] केवल दस्तावेज़ के मूल तत्व के लिए उपयोग किया जाता है। आप अन्य प्रकारों पर [XmlType] का उपयोग करना चाहते हैं।

इसके अलावा, आपको [Serializable] की आवश्यकता नहीं है। एक्सएमएल सीरिएलाइज़र इसे अनदेखा करता है।

+1

XmlType मेरी समस्या का समाधान हो की चोटी पर XMLType (AnonymousType = सच) का उपयोग आसान हो शायद के रूप में। धन्यवाद! –

0

मुझे एक समस्या में दो तृतीय पक्ष webservices के साथ यह समस्या थी। आश्चर्यजनक रूप से, गतिशील रनटाइम पीढ़ी ठीक थी (हालांकि इसमें 2 मिनट लग गए), लेकिन sgen.exe परेशान हो गया।

समाधान svcutil.exe उपयोग करने के लिए ...

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

तो यह संकलन करने Csc.exe का उपयोग किया गया था।

संबंधित मुद्दे