Find a child XML element. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Find a child XML element.

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w  w w .  java2s.  co m

public class Main{
        /// <summary>
        /// Find a child element.
        /// </summary>
        /// <param name="e">The element to search.</param>
        /// <param name="find">The name to search for.</param>
        /// <returns>The element found, or null if not found.</returns>
        public static XmlElement FindElement(XmlElement e, String find)
        {
            for (XmlNode child = e.FirstChild; child != null; child = child
                    .NextSibling)
            {
                if (!(child is XmlElement))
                {
                    continue;
                }
                XmlElement el = (XmlElement)child;
                if (el.Name.Equals(find))
                {
                    return el;
                }
            }
            return null;
        }
}

Related Tutorials