Hello evryone and thanks for your help i've looked into this question for a while, but i wasn't able to find a satisfactory answer.
This is the xml that i'm trying to deserialize :
<?xml version="1.0" encoding="UTF-8"?>
<base xmlns:xlink="http://www.w3.org/1999/xlink">
<entities>
<entity id="1" xlink:href="http://localhost"/>
<entity id="2" xlink:href="http://localhost"/>
</entities>
</base>
into this class :
[XmlRoot]
public class Entity
{
[XmlAttribute(AttributeName = "id")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "href")]
public string Href { get; set; }
}
[XmlRoot]
public class Entities
{
[XmlElement]
public List<Entity> Entity { get; set; }
}
[XmlRoot(ElementName = "base")]
public class Base_Entity
{
[XmlElement]
public Entities Entities { get; set; }
[XmlAttribute(AttributeName = "xlink")]
public string Xlink { get; set; }
}
Depending on the occasion i need to deserialize xmls in witch the property names are different but the mapping is always the same for example i can have.
<?xml version="1.0" encoding="UTF-8"?>
<base xmlns:xlink="http://www.w3.org/1999/xlink">
<factories>
<factory id="1" xlink:href="http://localhost"/>
<factory id="2" xlink:href="http://localhost"/>
</factories>
</base>
If i decorate my properties with element names it's working fine, but i'd really like to figure out how to do this without having to make individual classes for each different names. I'm deserializing like this :
var entity_serializer = new XmlSerializer(typeof(Base_Entity));
using (TextReader reader = new StringReader(toSerialize))
{
var myObject = (Base_Entity)entity_serializer.Deserialize(reader);
return myObject;
}
I'd expect that without naming the properties it'd deserialize fine but for some reason i'm getting null objects.