C#获取XML TAG VALUE

Rak*_*esh 7 c# xml

我有一个名为的xml文件 BackupManager.xml

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Settings>
<directory id="backUpPath" value="D:/BACKUPS/"></directory>
<filename id="feilname" value="SameName"></filename>
<period id ="period" value="15"></period>
</Settings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我试图从标签中获取值到字符串例如: - 我需要'backUpPath'标签的值为'D:/ BACKUPS /'到字符串说'str'

我试过的代码是

XmlDocument infodoc = new XmlDocument();
infodoc.Load("BackupManager.xml");
//int col = infodoc.GetElementsByTagName("directory").Count;
String str = infodoc.GetElementByID("directory").value;
Run Code Online (Sandbox Code Playgroud)

但我在'str'上得到零值

Pra*_*ana 7

试用

linq到xml的方式

IEnumerable<XElement> direclty = infodoc.Elements("Settings").Elements("directory");
var rosterUserIds = direclty .Select(r => r.Attribute("value").Value);
Run Code Online (Sandbox Code Playgroud)

要么

   XmlNodeList nodeList=
(infodoc.SelectNodes("configuration/Settings/directory"));

foreach (XmlNode elem in nodeList)
{
string strValue = elem.Attributes[1].Value;

}
Run Code Online (Sandbox Code Playgroud)