How to deserialize xml using Linq?

后端 未结 3 1180
梦谈多话
梦谈多话 2020-12-06 13:18

How can i deserialize this xml using Linq? I want to create List


  
    1
    

        
3条回答
  •  无人及你
    2020-12-06 14:03

    string xml = @"
                    
                        1
                        Step 1
                        Step 1 Description
                    
                    
                        2
                        Step 2
                        Step 2 Description
                    
                    
                        3
                        Step 3
                        Step 3 Description
                    
                    
                        4
                        Step 4
                        Step 4 Description
                    
                    ";
    
    XDocument doc = XDocument.Parse(xml);
    
    var mySteps = (from s in doc.Descendants("Step")
                   select new 
                    {
                        Id = int.Parse(s.Element("ID").Value),
                        Name = s.Element("Name").Value,
                        Description = s.Element("Description").Value
                    }).ToList();
    

    Heres how you would do it using LINQ. Obviously you should be doing your own error checking.

提交回复
热议问题