Liqu...">

Get attribute value from C # / xpath

I have an app.config file and you need to get the attribute value:

<param name="File" value="C:\"/> 

Liquid XML Studio provides the following xml:

 /configuration/log4net/appender/param[1] 

However, what C # code can xpath use to get the value?

+6
source share
4 answers

Use this XPath:

 /configuration/log4net/appender/param[@name='File']/@value 

Depending on how you read the XML, the use of XPath may be slightly different. If you use XDocument , you can use the XPathSelectElement extension XPathSelectElement . If you are using an XmlDocument , there is a SelectSingleNode method. And if you are using XPathDocument , you need to compile XPathExpression and use it against the navigator.

+16
source

You can use XmlDocument . See XmlNode.SelectSingleNode and others.

Example:

 XmlDocument doc = new XmlDocument(); doc.LoadXml(@"<configuration> <log4net> <appender> <param name=""File"" value=""C:\""/> </appender> </log4net> </configuration>"); var node = doc.DocumentElement.SelectSingleNode("//param[@name = 'File']/@value"); Console.WriteLine(node.Value); 
+4
source

I like....

  var result = XDocument.Load("test.xml").Descendants("param"); foreach (var p in result) { Console.WriteLine(p.Attribute("name")); } Console.Read(); 
+1
source

You can use XmlDocument and SelectSingleNode method - http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx
It will find the node matching your XPath.

I would do it with LINQ to XML (not with XPath)

0
source

Source: https://habr.com/ru/post/892815/


All Articles