Get XML Single Child Element - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Get XML Single Child Element

Demo Code


using System.Xml;
using System.Runtime.Remoting.Messaging;
using System.Linq;
using System;/*from   w  w w . j  a va2  s .c o  m*/

public class Main{
    public static XmlElement GetSingleChildElement(this XmlElement parentElement, string childElementName)
    {

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

        var childElement = GetSingleChildElement((XmlElement) childNode, childElementName);
        if (childElement != null)
          return childElement;
      }

      return null;
    }
}

Related Tutorials