Has XML Child Element - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Has XML Child Element

Demo Code


using System.Xml;
using System.Runtime.Remoting.Messaging;
using System.Linq;
using System;//ww w. j  a  va 2 s  .  c  o  m

public class Main{
        public static bool HasChildElement(this XmlElement element, string childElementName)
    {
      //var hasChildElement = element.ChildNodes.Cast<XmlNode>()
      //  .Any(node => node.NodeType == XmlNodeType.Element && node.LocalName == childElementName);

      foreach (var childNode in element.ChildNodes.Cast<XmlNode>())
      {
        if ((childNode.NodeType == XmlNodeType.Element && childNode.LocalName == childElementName) || HasChildElement((XmlElement)childNode, childElementName))
        {
          return true;
        }
      }

      return false;
    }
}

Related Tutorials