在XDocument节点中的Foreach

时间:2017-08-04 09:18:56

标签: c# asp.net

<tests>
  <test id ="1">
    <name> peter </name>
    <age> 23 </age>
    <informations> bla bla </informations>
  </test>
  <test id ="41">
    <name> besd </name>
    <age> 54 </age>
    <informations> some other text </informations>
  </test>
  <test id ="57">
    <name> john </name>
    <age> 61 </age>
    <informations> vintage </informations>
  </test>
  <test id ="67">
    <name> claude </name>
    <age> 11 </age>
    <informations> motivation </informations>
  </test>
</tests>

我设法在XDocument xInformations内获得了上述所有信息。

  List<string> testIds = new List<string>();
  testIds = xInformations.Descendants("test").Select(x => (string)x.Attribute("id")).ToList();

现在,我确实希望使用foreach来读取并保存每个id的所有信息:

foreach (string extId in testIds.Distinct())
{
     /// how can I take step by step the name, age, informations for all test cases ?
}

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:3)

创建匿名(或引入自己的类)实例并在foreach循环

中使用它
var tests = xInformations.Descendants("test")
                         .Select(x => 
                         {
                             new 
                             {
                                 Id = x.Attribute("id")?.Value,
                                 Name = x.Element("name").Value,
                                 Age = x.Element("age").Value,
                                 Info = x.Element("informations").Value
                             }
                         });

foreach(var test in tests)
{
    // test.Id
    // test.Name
    // test.Age
    // test.Info       
}

或者,如果xml文件的架构保持不变,则可以使用更清晰的代码与XmlSerializer

[XmlType("tests")]
public class Tests
{
    public List<Test> Tests { get; set; }
}

[XmlType("test")]
public class Test
{
    [XmlAttribute("id")]
    public int Id { get; set; }
    [XmlElement("name")]
    public string Name { get; set; }
    [XmlElement("age")]
    public int Age { get; set; }
    [XmlElement("informations")]
    public string Info { get; set; }
}

var serializer = new XmlSerializer(typeof(Tests));
Tests tests = null;
using (var reader = new StreamReader(pathToXmlFile))
{
    tests = (Tests)serializer.Deserialize(reader);
}

// use tests for your needs
foreach(var test in tests.Tests)
{
    // test.Id
}

答案 1 :(得分:0)

这是你可以做到的一种方式

        // path to file
        string path = @"C:\t\1.txt";
        XDocument _doc = XDocument.Load(path);

        List<XElement> testIds = new List<XElement>();
        // get all test elements
        testIds = _doc.Descendants("test").ToList();

        // loop true test elements
        foreach (XElement extId in testIds.Distinct())
        {
            // get element values

            string id = extId.Attribute("id").Value;
            string name = extId.Element("name").Value;
            string age = extId.Element("age").Value;
            string info = extId.Element("informations").Value;
            Console.WriteLine(id+ "," + name + ", " + " " + age + ", " + info);
        }

答案 2 :(得分:0)

string path = @"D:\1.txt";
  XDocument _doc = XDocument.Load(path);

  List<XElement> testIds = new List<XElement>();

  testIds = _doc.Descendants("test").ToList();


  foreach (XElement extId in testIds.Distinct())
  {
     // get element values
        string id = extId.Attribute("id").Value;
        string name = extId.Element("name").Value;
        string age = extId.Element("age").Value;
        string info = extId.Element("informations").Value;
        Console.WriteLine(name + ", " + " " + age + ", " + info);
  }
  

string id = extId.Attribute(“id”)。Value;

enter image description here

Attribute方法用于获取属性值内的节点值。

我被检查数据是否正确地来自属性。