开发者

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

I'm trying to check if a user exists from an XML response.

When a user doesn't exist the response is like this:

<ipb></ipb>

What would be the best way for me to (in code) verify that a user doesn't exist? I was thinking of checking if it didn't have any child elements but I'm somewhat confused.

Thanks for the help!

        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
}

or

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜