XDocument 라이브러리를 사용하여 XML 문서를 읽고 C#의 LINQ 목록을 쿼리합니다. 나는 암묵적인 var에 무의미한 목록을 반환합니다. 임의의 유형은 단순한 구조입니다. 내가 뭘 잘못하고 있는지 잘 모르겠지만, 이미 정의한 클래스 목록으로 반환하고 싶지만이 익명 형식을 사용자 정의 클래스로 변환하는 중 오류가 발생합니다. 이것에 대한 명백한 것이 빠져 있습니다. 여기에 동적 타이핑을 사용하고 싶지 않습니다.목록을 사용자 지정 개체로 변환
public class CustomClass
{
public string id { get; set; }
public int fileName { get; set; }
}
XDocument optXML = XDocument.Load(pathName);
var optInput = (from item in optXML.Descendants("Group")
select new
{
id = (int)item.Attribute("ID"),
fileName = (string)item.Attribute("FileName")
}).ToList();
foreach (CustomClass item in optInput)
{
Console.WriteLine(item.id);
}
("그룹")) item.Attribute ("ID")를 새로운 { ID = (INT를 선택 optXML.Descendants의 항목에서 (목록 = 같은 사용자 정의 클래스에 직접 가져올 수있는 방법이 있나요, fileName = (문자열) item.Attribute ("FileName") }). ToList(); –
JBone
답변을 업데이트했습니다. – Adil