0
我想知道如何檢查每個特定元素是否存在。
我寫了下面的代碼,但我不認爲這很聰明。如何檢查每個特定元素是否存在
if(xmlDoc.Descendants("ElementA").Any() && xmlDoc.Descendants("ElementB").Any() && ....
我想知道如何檢查每個特定元素是否存在。
我寫了下面的代碼,但我不認爲這很聰明。如何檢查每個特定元素是否存在
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()))
{
}
很好!謝謝!! – Nigiri
如果您需要驗證超過3個元素,我建議根據相應的Xsd模式進行驗證。 – Filburt