How can I use LINQ to retrieve a specific value of Supported attribute nased on the condition URL="localhoist"? Thank you.
<SomeSett开发者_Python百科ing>
  <Setting URL="abc.com" Supported="sb.new,mgrsma" />
  <Setting URL="localhost" Supported="GG,LLmgrsma,FF1,FF3" />
  <Setting URL="def.zxy.com" Supported="xyz" />
</SomeSetting>
Like this:
var localhost = doc.Descendants("Setting")
                   .Where(x => (string) x.Attribute("URL") == "localhost")
                   .Select(x => (string) x.Attribute("Supported"))
                   .FirstOrDefault();
One line at a time:
- First select all the "Setting" elements; you could also do this using someSetting.Elements("Setting")wheresomeSettingis theSomeSettingelement
- Add a where clause to filter out elements which don't have a URL of localhost. I'm using the explicit string conversion rather than the Valueproperty so that it copes with elements without theURLattribute; in that case the conversion will return null
- Select the Supportedattribute value - again, this will return null if aURL=localhostelement has noSupportedattribute.
- Select the first result, or null if there were no results. If you may have multiple elements and want to examine all of them, just remove this call.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论