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, 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With