Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 523
Martin Avatar asked Feb 11 '11 10:02

Martin


3 Answers

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
like image 56
Steve B Avatar answered Nov 14 '22 19:11

Steve B


[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.

like image 34
John Saunders Avatar answered Nov 14 '22 19:11

John Saunders


You have to decorate the fields also, e.g.:

[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();
}
like image 33
Vijay Sirigiri Avatar answered Nov 14 '22 17:11

Vijay Sirigiri