title: Generic class to Select element values in Xml to properties of a class
body: <p>I have an xml like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Property&gt;
    &lt;Owner&gt;
      &lt;FirstName&gt;LeBron&lt;/FirstName&gt;
      &lt;LastName&gt;James&lt;/LastName&gt;
    &lt;/Owner&gt;
    &lt;Seller&gt;
      &lt;Name&gt;LeBron&lt;/Name&gt;
      &lt;Code&gt;Seller01&lt;/Code&gt;
      &lt;Branch&gt;demoBranch&lt;/Branch&gt;
    &lt;/Seller&gt;
    &lt;Equipments&gt;
        &lt;Equipment&gt;
          &lt;Name&gt;Kodle&lt;/Name&gt;
          &lt;CountryOfOrigin&gt;Bryant&lt;/CountryOfOrigin&gt;
        &lt;/Equipment&gt;
        &lt;Equipment&gt;
          &lt;Name&gt;Desktop&lt;/Name&gt;
          &lt;CountryOfOrigin&gt;Kryon&lt;/CountryOfOrigin&gt;
        &lt;/Equipment&gt;
      &lt;/Equipments&gt;
  &lt;/Property&gt;
  </code></pre>
  
  <p>and classes:</p>
  
  <pre><code>public Property()
  {
      public Owner owner{get;set;}
      public Seller seller{get;set;}
      public equipment equipment{get;set;}
  }
  
  public Owner
  {
      public FirstName{get;set;}
      public LastName{get;set;}
  }
  public Seller
  {
      public Name{get;set;}
      public Code{get;set;}
      public Branch{get;set;}
  }
  public Equipment
  {
      public Name{get;set;}
      public CountryOfOrigin{get;set;}
  }
  </code></pre>
  
  <p>Of course, we can load the class from the xml one by one,like:</p>
  
  <pre><code>    XDocument xDoc = XDocument.Load(xmlFile);     
  
      var owner = from e in doc.Descendants("Owner")
                  select new Owner
                  {
                      FirstName = e.Element("FirstName").Value,
                      LastName = e.Element("LastName").Value
                  };   
  </code></pre>
  
  <p>or deserialize the xml and get the sub class.</p>
  
  <pre><code>    public Property LoadFromDocument(string fileUrl){
          Property serializableObject = null;
           using(TextReader textReader = textReader = new StreamReader(fileUrl)){
               XmlSerializer xmlSerializer = new XmlSerializer(ObjectType);
               serializableObject = xmlSerializer.Deserialize(textReader) as Property;
          }
          return serializableObject;
      }
  </code></pre>
  
  <p>and then get owner by </p>
  
  <pre><code>Owner owner = LoadFromDocument(filrUrl).Owner;
  </code></pre>
  
  <p>but I want to create a generic class to do like bellow.</p>
  
  <pre><code>public class OXmlRepository&lt;T&gt;
  {
  
      public XDocument xDoc { get; set; }
       protected XName ElementName { get; private set; }
       protected OXmlRepository()
      {
          ElementName = typeof(T).Name.ToString();
      }
  
      public T GetObject()
      {
              //how can I get the class T instance with the values from Xml
      }
  
  }
  </code></pre>