c# - Linq-to-XML for retrieving all child nodes -
i have following xml
<?xml version="1.0" encoding="utf-8"?> <errorserver> <clientip> <allowall>false</allowall> <client_127_0_0_1>true</client_127_0_0_1> </clientip> <users> <admin> <password>passw0r!d</password> <nexterror>83</nexterror> <active>true</active> </admin> <jimbob> <password>passw0r!d</password> <nexterror>83</nexterror> <active>true</active> </jimbob> </users> </errorserver>
using linq in c# trying user names (admin & jimbob in example above) using following code
list<string> result = new list<string>(); xdocument xdoc = xdocument.load("errorserverconfig.xml"); //run query var lv1s = lv1 in xdoc.descendants("errorserver") select new { children = lv1.elements("users") }; //loop through results foreach (var lv1 in lv1s) { foreach (var lv2 in lv1.children) result.add(lv2.name.tostring()); } return (result);
which isn't working returns "users" in result.
i new linq can tell me correct way of doing please?
var result = xdoc.descendants("users") .first() .elements() .select(e=>e.name);
Comments
Post a Comment