简体   繁体   中英

XML Serialization error: 2 types both use the XML type name, 'Relationship', from namespace ''

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

[XmlRoot] is only used for the root element of the document. You want to use [XmlType] on other types.

Also, you don't need [Serializable] . The XML Serializer ignores it.

You have to decorate the fields also, eg:

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

I had this problem with two 3rd party webservices I was consuming in one application. Strangely, the dynamic runtime generation was fine (although it took 2 minutes), but sgen.exe got upset.

The solution was to use svcutil.exe...

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

Then use csc.exe to compile it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM