0

特定の要素がすべて存在するかどうかを確認する方法を知りたいです。
私は次のコードを書きましたが、それは賢いとは思いません。

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

1 に答える 1

2

あなたはこれを行うことができます:

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

可能であれば、メンバーを保存することをお勧めします。

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

毎回再作成する代わりに。次に、次のことができます。

if (ELEMENTS.All(element => xmlDoc.Descendants(element).Any()))
{
}
于 2012-05-08T06:34:17.760 に答える