tags:

views:

36

answers:

1

How to convert XML Elements to of type Person ?

Elements :

XElement persons = XElement.Parse(
               @"<persons>
                   <person>
                        <id>10001</id>     
                       <name> Daniel </name> 
                   </person>
                   <person>
                        <id>10002</id>     
                       <name>Marshal</name> 
                   </person>
                    <person>
                        <id>10003</id>     
                       <name>Leo</name> 
                   </person>
                 </persons>"
            );

Person Type:

class Person
    {
        int personID;
        string name;

        public int PersonID
        {
            get {return personID;}
            set {personID = value;}
        }

        public string Name
        {
            get {return name;}
            set {name = value;}
        }
    }

I tried (incomplete & not sure whether valid approach )

 Person[] Prns = from perns in persons.Nodes select new {perns};
+4  A: 
var personList =
    from p in persons.Elements("person")
    select new Person
    {
        Name = p.Element("name").Value,
        PersonID = int.Parse(p.Element("id").Value)
    };
cxfx
Fine.Thanks for spending your time.Working well.