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.

2
  • Does this help? stackoverflow.com/questions/28769495/… Commented Apr 5, 2016 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. Commented Apr 6, 2016 at 14:29

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.