linq을 사용하여 매우 간단한 XML 파일을 읽으려고합니다. 나는 자식 노드를 통해 루프를 사용하는 방법을 알고 있지만 linq을 배우려고 노력하고 있습니다. 어쨌든 파일이 있습니다 :간단한 XML 파일 읽기
<firstElement>
<Setting Name="appDataPath" Value="some\\path" />
<Setting Name="appConfigFileName" Value="somename.xml" />
<Setting Name="appConfigFilePath" Value="some\\path" />
</firstElement>
그리고 설정 속성을 원합니다. 이 코드는 다음과 같습니다.
//Load xml
XDocument xdoc = XDocument.Load(fullyQualifiedPath);
var settings = from item in xdoc.Descendants("firstElement")
select new
{
name = item.Attribute("Name").Value,
val = item.Attribute("Value").Value
};
// create a dictionary
Dictionary<string, string> settingsDictionary =
new Dictionary<string, string>();
//Loop through results
foreach (var setting in settings)
{
settingsDictionary.Add(setting.name, setting.val);
}
xdoc.Save(fullyQualifiedPath);
어떤 이유로 인해 쿼리 후에 설정이 null입니다. 내가 놓친 게 있니?
'eStack'이란 무엇입니까? –
'xdoc.Save (fullyQualifiedPath);가 당신의 문제와 관련이 있습니까? 저장 후 디스크의 xml 문서에서 비어 있습니까? 이것이 문제입니까? 이 줄을 제거하지 않으면 질문에만 혼동을줍니다. 또한'Descendants ("eStack")'부분을 가지고 있지만 조각에'eStack' 노드가 없습니다. – Igor
이것을 시도하십시오 : 'from item in xdoc.Descendants ("Setting")' –