c# - some issue in this linq to xml query, while getting value of certain attribute in an element -
i have app.config file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appsettings> <add key="domain" value="localhost"/> <add key="hostname" value="hostpc"/> </appsettings> </configuration>
i using linq xml query value of key named "domain". instance, when want value of key named "domain" should give me "localhost" :
var domain = (from appnode in xmlelement.elements("add") appnode.attribute("key").value == "domain" select appnode.attribute("value"));
but query missing can identify missing or how can made better, doesn't work @ moment.
note: want use linq xml
i used app.config file content is, write small console based application following code:
class program { static void main(string[] args) { // create query var nodes = appnode in xelement.load("app.config").descendants("appsettings").elements() appnode.attribute("key").value == "domain" select appnode; var element = nodes.firstordefault(); string value = element.attribute("value").value; console.writeline(value); //pause application console.readline(); } }
the output is: localhost
hope helps. if not work on machine, please share error details.
Comments
Post a Comment