이 코드를 실행할 때마다 certlist는 첫 번째 값 집합을 읽고 성공적으로 목록에 씁니다. 루프를 다시 실행할 때 다음 값 집합이 첫 번째 값을 덮어 쓰고 두 번째 값을 만듭니다. 최종 결과는 목록 내에서 두 개의 동일한 값입니다.목록이 올바르게 채워지지 않음
첫 번째 값을 덮어 쓰는 이유와 해결 방법에 대한 도움이 될 것입니다.
foreach (var certcard in xdoc.Root.Element("Diver").Element("Certifications").Elements("Certification_Card"))
{
cert.Level = certcard.Element("Level").Value;
cert.Agency = certcard.Element("Agency").Value;
cert.Number = certcard.Element("Number").Value;
cert.Date = Convert.ToDateTime(certcard.Element("Date").Value);
certlist.Add(cert);
}
'cert'를 루프 안에 선언하십시오. –