C# GET XML TAG VALUE

c#, xml

Solution

try out

linq to xml way

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

OR

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

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

}

Problem

I have an xml file named `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> ``` I am trying to get value from the tags to a string Eg:- I need value of 'backUpPath' tag as 'D:/BACKUPS/' to a string say 'str' The code I have tried is ``` XmlDocument infodoc = new XmlDocument(); infodoc.Load("BackupManager.xml"); //int col = infodoc.GetElementsByTagName("directory").Count; String str = infodoc.GetElementByID("directory").value; ``` But i am getting null value on 'str'

Original source