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