c# - Query XML source in LinqPad using lambda syntax -
considering following xml data stored in file xmldata.xml, trying use linqpad query using lambda syntax.
i remember using before can't figure out again. query data using properties if querying object in visual studio.
<?xml version="1.0"?> <arrayofhrops_user xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <hrops_user> <entitykey> <entitysetname>hrops_user</entitysetname> <entitycontainername>hroperationsentities</entitycontainername> <entitykeyvalues> <entitykeymember> <key>userid</key> <value xsi:type="xsd:int">44405</value> </entitykeymember> </entitykeyvalues> </entitykey> <userid>44405</userid> <employeeid>aaa40</employeeid> <period>2015-06-17t00:00:00</period> <active>true</active> <options>false</options> <pager>false</pager> <contractor>false</contractor> <timestamp>2015-06-18t13:37:38.3</timestamp> <username>mark.walsh</username> </hrops_user> <hrops_user> <entitykey> <entitysetname>hrops_user</entitysetname> <entitycontainername>hroperationsentities</entitycontainername> <entitykeyvalues> <entitykeymember> <key>userid</key> <value xsi:type="xsd:int">44406</value> </entitykeymember> </entitykeyvalues> </entitykey> <userid>44406</userid> <employeeid>aaa60</employeeid> <period>2015-06-17t00:00:00</period> <active>true</active> <options>false</options> <pager>false</pager> <contractor>false</contractor> <timestamp>2015-06-18t13:37:38.94</timestamp> <username>mark.walsh</username> </hrops_user> in linqpad - these statements load data , output correctly:
var myxml = xelement.load (@"c:\temp\xmldata.xml"); myxml.elements().dump(); i expected following work too:
myxml.elements().firstordefault(x=>x.username == "mark.walsh").dump(); but gives me error:
'system.xml.linq.xelement' not contain definition 'username' , no extension method 'username' accepting first argument of type 'system.xml.linq.xelement' found (press f4 add using directive or assembly reference)
again, remember having nice clean syntax before, can't figure out how went. thanks!
probably meant using element() method passing element name parameter :
myxml.elements() .firstordefault(x => (string)x.element("username") == "mark.walsh") .dump();
Comments
Post a Comment