C# 使用C从XML检索键值对中的字段

C# 使用C从XML检索键值对中的字段,c#,xml,selenium,selenium-webdriver,xml-parsing,C#,Xml,Selenium,Selenium Webdriver,Xml Parsing,我有一个XML,如下所示: <test-run> <test-suite> <test-suite> <test-case id="1234" name="ABC" result="Passed"> </test-case> </test-suite> </test-suite> </test-run> 这是我正在使用的一个示例XML文件。 如何使用C从中检索id、名称和结果?使

我有一个XML,如下所示:

<test-run>
 <test-suite>
 <test-suite>
   <test-case id="1234" name="ABC" result="Passed">
   </test-case>
 </test-suite>
 </test-suite>
</test-run>
这是我正在使用的一个示例XML文件。 如何使用C从中检索id、名称和结果?

使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Result> results = doc.Descendants("test-case").Select(x => new Result()
            {
                id = (string)x.Attribute("id"),
                name = (string)x.Attribute("name"),
                result = (string)x.Attribute("result")
            }).ToList();
        }
    }
    public class Result
    {
        public string id { get; set; }
        public string name { get; set; }
        public string result { get; set; }
    }
}

1这不是有效的XML-2我看不到您的代码