问题
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();
}
回答1:
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
回答2:
[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.
回答3:
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();
}
回答4:
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.
来源:https://stackoverflow.com/questions/4967941/xml-serialization-error-2-types-both-use-the-xml-type-name-relationship-fro