使用 ASP.NET mvc5 显示 xml 文件中的特定记录

Display a specific record from an xml file using ASP.NET mvc5

我无法弄清楚这里出了什么问题。它给我一个错误是节点在其上下文中不存在:

XmlDocument doc = new XmlDocument();
doc.Load("path");

Personal persons = doc.SelectNodes("/Persons/record")
 .Cast<XmlNode>()
 .Where(ID.Equals(node["ID"].InnerText) ==> This node does not exist
 .Select(node => new Personal()
 {
     ID = node["ID"].InnerText,
     Name = node["Name"].InnerText,
     Email = node["Email"].InnerText,
     DateOfBirth = node["DateOfBirth"].InnerText,
     Gender = node["Gender"].InnerText,
     City = node["City"].InnerText
 }).FirstOrDefault());

return View(persons);

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
  <record>
    <ID>1602081497499</ID>
    <Name>Graham, Echo J.</Name>
    <Email>diam@Nullaeu.net</Email>
    <DateOfBirth>11/07/93</DateOfBirth>
    <Gender> Female</Gender>
    <City>Lloydminster</City>
  </record>
  <record>
    <ID>1688110330299</ID>
    <Name>Larson, Kevin K.</Name>
    <Email>eu.augue@penatibuset.co.uk</Email>
    <DateOfBirth>03/11/94</DateOfBirth>
    <Gender>Male </Gender>
    <City>Habra</City>
  </record>
</Persons>

我试过将其更改为 xmlNodecontains 等。请并感谢您的帮助。

这是一个使用 Xml Linq 的解决方案,它是一个比旧 XmlElement

新的 Visual Studio 库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication71
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string id = "123";

            XDocument doc = XDocument.Load(FILENAME);
            List<Personal> personals = doc.Descendants("record").Select(x => new Personal()
            {
                ID = (string)x.Element("ID"),
                Name = (string)x.Element("Name"),
                Email = (string)x.Element("Email"),
                DateOfBirth = (DateTime)x.Element("DateOfBirth"),
                Gender = (string)x.Element("Gender"),
                City = (string)x.Element("City")
            }).Where(x => x.ID == id).ToList();
        }

    }
    public class Personal
    {
        public string ID {get ; set; }
        public string Name {get ; set; }
        public string Email {get ; set; }
        public DateTime DateOfBirth {get ; set; }
        public string Gender {get ; set; }
        public string City { get; set; }
    }
}