0

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.

Terence
  • 1
  • 1
  • @TerenceN'Diaye: See this post: [https://stackoverflow.com/a/24708143/6630084](https://stackoverflow.com/a/24708143/6630084) – Jackdaw Mar 13 '23 at 16:14
  • @Jackdaw thanks but i don't think this post is usable in my case, i have well overs 50 differents name possibilities and it just just does not match the usecase i'm in. i'd like to reach a more generic approach – Terence Mar 13 '23 at 16:23
  • Another possibility would be to use the XmlAnyElementAttribute as shown here: https://stackoverflow.com/a/30925157/880990 – Olivier Jacot-Descombes Mar 13 '23 at 16:37

1 Answers1

1

I think all that you really need is a list of Entity

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    var nodes = doc.DocumentElement.ChildNodes.OfType<XmlElement>()
                                   .First().Cast<XmlElement>();

    List<Entity> entities = nodes
    .Select(node => new Entity
    {
        Id = Convert.ToInt32((string)node.Attributes[0].Value),
        Href = (string)node.Attributes[1].Value
    }).ToList();
Serge
  • 40,935
  • 4
  • 18
  • 45