繁体   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