0

This is my XML structure:

<codelang1>
       <prox>prox dans la langue</prox>
       <libelle>libellé dans la langue</libelle>
       <descriptif>descriptif dans la langue</descriptif>
</codelang1>
<codelang2>
       <prox>prox dans la langue</prox>
       <libelle>libellé dans la langue</libelle>
       <descriptif>descriptif dans la langue</descriptif>
</codelang2>
...
<codelang...n>
       <libelle></libelle>
       ....
</codelang...n>

How can I serialize this XML with a class c#? The codelang changes.

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
user1428798
  • 1,534
  • 3
  • 24
  • 50
  • Does this help? https://stackoverflow.com/questions/28769495/how-to-derive-xml-element-name-from-an-attribute-value-of-a-class-using-annotati – dbc Apr 05 '16 at 16:11
  • 1
    Doesn't look there's a simple way of doing this. The above link is the best you'll get. Its a shame you can't add something like [XmlElement("codelang*")] i.e. use a wildcard in the element name. – Tim Rutter Apr 06 '16 at 14:29

1 Answers1

0

Bit late but try this.....

Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

Classes

public class Codelang
{
    public string prox { get; set; }
    public string libelle { get; set; }
    public string descriptif { get; set; }
}

Code

class Program
{
    static void Main(string[] args)
    {
        string xmlfilepath = @"xml.xml";
        XDocument xmlsrcdoc = XDocument.Load(xmlfilepath);
        List<Codelang> lstCodelang = new List<Codelang>();

        try
        {
            lstCodelang = xmlsrcdoc.Descendants()
                                       .Elements("prox")
                                       .Select(el => FromXElement<Codelang>(el.Parent))
                                       .ToList();

        }
        catch (Exception)
        {

            throw;
        }
    }

    public static T FromXElement<T>(XElement element) where T : class, new()
    {
        var typeOfT = typeof(T);
        T value = new T();
        foreach (var subElement in element.Elements())
        {
            var prop = typeOfT.GetProperty(subElement.Name.LocalName);
            if (prop != null)
            {
                prop.SetValue(value, subElement.Value);
            }
        }
        return value;
    }
}

XML

<root>
<codelang1>
       <prox>prox dans la langue 1</prox>
       <libelle>libellé dans la langue</libelle>
       <descriptif>descriptif dans la langue</descriptif>
</codelang1>
<codelang2>
       <prox>prox dans la langue 2</prox>
       <libelle>libellé dans la langue</libelle>
       <descriptif>descriptif dans la langue</descriptif>
</codelang2>
<codelang3>
       <prox>prox dans la langue 3</prox>
       <libelle>libellé dans la langue</libelle>
       <descriptif>descriptif dans la langue</descriptif>
</codelang3>
</root>

I am reading your XML in to a string from a file in the application build folder called xml.xml... you will need to get the XML string from somewhere else or create the xml.xml file and save your XML for the code above to work

Monty
  • 1,534
  • 2
  • 10
  • 12