我如何使用Linq反序列化此xml?
我想创建List<Step>

<MySteps>
  <Step>
    <ID>1</ID>
    <Name>Step 1</Name>
    <Description>Step 1 Description</Description>
  </Step>
  <Step>
    <ID>2</ID>
    <Name>Step 2</Name>
    <Description>Step 2 Description</Description>
  </Step>
  <Step>
    <ID>3</ID>
    <Name>Step 3</Name>
    <Description>Step 3 Description</Description>
  </Step>
  <Step>
    <ID>4</ID>
    <Name>Step 4</Name>
    <Description>Step 4 Description</Description>
  </Step>
</MySteps>

最佳答案

string xml = @"<MySteps>
                <Step>
                    <ID>1</ID>
                    <Name>Step 1</Name>
                    <Description>Step 1 Description</Description>
                </Step>
                <Step>
                    <ID>2</ID>
                    <Name>Step 2</Name>
                    <Description>Step 2 Description</Description>
                </Step>
                <Step>
                    <ID>3</ID>
                    <Name>Step 3</Name>
                    <Description>Step 3 Description</Description>
                </Step>
                <Step>
                    <ID>4</ID>
                    <Name>Step 4</Name>
                    <Description>Step 4 Description</Description>
                </Step>
                </MySteps>";

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();

继承人如何使用LINQ做到这一点。显然,您应该进行自己的错误检查。

10-08 02:22