简体   繁体   中英

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM