C#: Check particular attribute key-value pair is present using linq -


i want check whether particular attribute key-value pair present using linq.
using following code check:

bool keypresent = xdocument.element("nosqldb").element("elements").elements("key")                            .where(el => el.attribute("id").equals(key.tostring()))                            .any();   

my xml looks below:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <nosqldb>   <keytype>system.int32</keytype>   <payloadtype>a</payloadtype>   <elements>     <key id="1">       <name>element</name>       <descr>test element</descr>       <timestamp>2015-10-02t23:54:07.6562371-04:00</timestamp>       <children>         <item>1</item>         <item>2</item>         <item>3</item>       </children>       <payload>         <item>cse681</item>         <item>sma</item>         <item>c#.net</item>       </payload>     </key>   </elements> </nosqldb> 

you use xpath try find element.

bool keypresent = xdocument.xpathselectelement(string.format("//key[@id={0}]", key)) != null; 

..or using linq

bool keypresent = xdocument.descendants()     .any(e => e.name == "key" && e.attributes()         .any(a => a.name == "id" && a.value == key.tostring())); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -