LINQ to XML. How to get some string?

后端 未结 2 659
旧巷少年郎
旧巷少年郎 2021-01-25 18:29

I have xml:



  
    Ray
    Other         


        
2条回答
  •  野性不改
    2021-01-25 19:33

    When using Elements() you have to specify the structure more precisely.

    In your code, cover is a element. But size is an attribute of .

        var c = from cover in xml.Elements("book")
                    where (string)cover.Attribute("size").Value == "mini"
                    select cover.Value;
    

    This ought to work:

        var c = from cover in xml.Elements("book")
                        .Elements("cover")
                    where (string)cover.Attribute("size").Value == "mini"
                    select cover.Value;
    

提交回复
热议问题