简体   繁体   中英

How to check if every certain elements exists

I would like to know how to check if every certain elements exists.
I wrote the following code but I don't think that is smart.

if(xmlDoc.Descendants("ElementA").Any() && xmlDoc.Descendants("ElementB").Any() && ....

You could do this:

if (new[] {"ElementA", "ElementB", "ElementC"}
           .All(element => xmlDoc.Descendants(element).Any()))
{
}

And if you can, I suggest saving a member:

private static readonly string[] ELEMENTS = new string[]
                                                {
                                                    "ElementA",
                                                    "ElementB",
                                                    "ElementC"
                                                };

instead of recreating it everytime. Then you could do:

if (ELEMENTS.All(element => xmlDoc.Descendants(element).Any()))
{
}

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