简体   繁体   中英

C# Linq to XML getting the elements where value of name contains a specific string

I have an XML file that contains following type of elements:

<add name="$(ReferAEP)" value="$(addressA)" />
<add name="$(ReferBEP)" value="$(addressB)" />

what is the linq to xml query to get only elements where "name" attribute contains a value like *EP ? so the above two elements will end up in the result?

If you have an outer element for the xml tags specified in your question like:

<adds>
    <add name="$(ReferAEP)" value="$(addressA)" />
    <add name="$(ReferBEP)" value="$(addressB)" />
</adds>

Then use the below linq query:

var query = from d in xdoc.Descendants("adds").Descendants()
            where d.Attribute("name")?.Value.Contains("EP")
            select d;

您在寻找类似的东西吗

document.Descendants("add").Where(element => element.Attribute("name").Value.EndsWith("EP)"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM