How to check if a tag exists in an xml?

Asked

Viewed 3,577 times

3

// Obtém a tag "tags"
if(xmlDoc.ChildNodes[1].ChildNodes[1].ChildNodes[1].SelectSingleNode("tags") != null)
{

}

I am running an Xml and in some we do not have this tag "tags" and error.

Does anyone know another way to validate this ?

1 answer

3

var node = xmlEnvio.GetElementsByTagName("tag").OfType<XmlNode>();

if (node != null && node.Count() > 0)
{
    foreach (XmlNode item in node)
    {
        if (item["tags"] != null)
        {
            //Código
        }
    }
}

I use this form to validate us.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.