Finding element in XDocument?

前端 未结 7 1620
眼角桃花
眼角桃花 2020-12-02 20:02

I have a simple XML


  
    greatest Band
            


        
7条回答
  •  不知归路
    2020-12-02 20:32

    Elements() will only check direct children - which in the first case is the root element, in the second case children of the root element, hence you get a match in the second case. If you just want any matching descendant use Descendants() instead:

    var query = from c in xmlFile.Descendants("Band") select c;
    

    Also I would suggest you re-structure your Xml: The band name should be an attribute or element value, not the element name itself - this makes querying (and schema validation for that matter) much harder, i.e. something like this:

    
      
      regular Band
      1
      2
    
    

提交回复
热议问题