特定の要素がすべて存在するかどうかを確認する方法を知りたいです。
私は次のコードを書きましたが、それは賢いとは思いません。
if(xmlDoc.Descendants("ElementA").Any() && xmlDoc.Descendants("ElementB").Any() && ....
あなたはこれを行うことができます:
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()))
{
}