簡體   English   中英

如何檢查XDocument是否至少有一個孩子?

[英]How can I check if an XDocument has at least one child?

我正在嘗試從XML響應中檢查用戶是否存在。

當用戶不存在時,響應如下所示:

<ipb></ipb>

對我而言(以代碼形式)驗證用戶不存在的最佳方式是什么? 我當時正在考慮檢查它是否沒有任何子元素,但我有些困惑。

謝謝您的幫助!

        public void LoadUserById(string userID)
    {
        doc = XDocument.Load(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", userID));

        if (doc.DescendantNodes().ToList().Count < 1)
        {
            userExists = false;
        }
        else 
        {
            userExists = true;
        }
    }
if (doc.Root.Elements().Any())
{
    // User was found
}

要么

XElement profile = doc.Root.Element("profile");
if (profile != null)
{
    // User was found
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM