C # GET XML TAG VALUE

I have an xml file called 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'm trying to get the value from the tags into a string For example: - I need the tag value 'backUpPath' as ​​'D: / BACKUPS /' for the string say 'str'

The code I tried is

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

But I get a null value on 'str'

+6
source share
7 answers

try

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; } 
+5
source

Because you do not have an element with the identifier "directory". Or do you want

 GetElementByID("backUpPath").GetAttribute("value"); 

or

 GetElementsByTagName("directory"); 

Remember that the second method returns an XMLNodeList!

0
source

if you want u to be able to use XmlReader

  string str =""; using (var reader = new StreamReader(BackupManager.xml)) { var all = reader.ReadToEnd(); StringReader stringReader = new StringReader(all); XmlReader xmlReader = XmlTextReader.Create(stringReader,new System.Xml.XmlReaderSettings() { IgnoreWhitespace = true, IgnoreComments = true }); while (xmlReader.Read()) if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "directory") str = xmlReader["value"]; } 
0
source
 XmlDocument infodoc = new XmlDocument(); infodoc.Load("BackupManager.xml"); XmlElement directoryElement = document.GetElementById("directory"); string backupPath = directoryElement.GetAttribute("value"); 
0
source
 if (xml.NodeType == XmlNodeType.Element && xml.Name == "Architecture") { string architecture = xml.ReadElementContentAsString(); } 
0
source

In the past, I had to deal with huge XML and performance was a problem. All I needed was not cached, read-only, read-only access to XML.

In addition, I did not have control over the scheme, I just needed to squeeze certain tag values ​​from XML and and CDATA .

Below I get:

 private string GetValueFromXmlTag(string xml, string tag) { if (xml == null || tag == null || xml.Length == 0 || tag.Length == 0) return ""; string startTag = "<" + tag + ">", endTag = "</" + tag + ">", value = null; int startTagIndex = xml.IndexOf(tag, StringComparison.OrdinalIgnoreCase), endTagIndex = xml.IndexOf(endTag, StringComparison.OrdinalIgnoreCase); if (startTagIndex < 0 || endTagIndex < 0) return ""; int valueIndex = startTagIndex += startTag.Length - 1; try { value = xml.Substring(valueIndex, endTagIndex - valueIndex); } catch (ArgumentOutOfRangeException responseXmlParserEx) { string err = string.Format("Error reading value for \"{0}\" tag from XXX XML", tag); log.Error(err, responseXmlParserEx); } return (value ?? ""); } 
0
source
 XmlDocument infodoc = new XmlDocument(); //Server.MapPath() return the xml file address infodoc.Load(Server.MapPath("~/XMLFile1.xml")); XmlNodeList nodeList = (infodoc.SelectNodes("configuration/Settings/backUPpath")); foreach (XmlNode elem in nodeList) { Response.Write(elem.Attributes[1].Value); } 
0
source

All Articles