如何使用Linq反序列化xml?
如何使用Linq反序列化这个xml? 我想创建List
1 Step 1 Step 1 Description 2 Step 2 Step 2 Description 3 Step 3 Step 3 Description 4 Step 4 Step 4 Description
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();
inheritance人如何使用LINQ来做到这一点。 显然你应该做自己的错误检查。
LINQ-to-XML是你的答案。
List steps = (from step in xml.Elements("Step") select new Step() { Id = (int)step.Element("Id"), Name = (string)step.Element("Name"), Description = (string)step.Element("Description") }).ToList();
还有一些关于从Scott Hanselman的 XML转换
在LINQ方法语法中显示以上答案
后人:
var steps = xml.Descendants("Step").Select(step => new { Id = (int)step.Element("ID"), Name = step.Element("Name").Value, Description = step.Element("Description").Value });
内容:
var steps2 = xml.Element("MySteps").Elements("Step").Select(step => new { Id = (int)step.Element("ID"), Name = step.Element("Name").Value, Description = step.Element("Description").Value });