Returns trimmed inner text of given parent element's child element with given name. If there is no such element returns null. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Returns trimmed inner text of given parent element's child element with given name. If there is no such element returns null.

Demo Code


using System.Xml;

public class Main{
        /// <summary>
        /// Returns trimmed inner text of given parent element's child element with given name.
        /// If there is no such element returns null.
        /// </summary>
        /// <param name="parentElement"></param>
        /// <param name="elemName"></param>
        /// <returns></returns>
        public static string ReadElementValue(XmlElement parentElement, string elemName)
        {/*from w ww. ja v a 2  s. co m*/
            XmlElement elem = (XmlElement)parentElement.SelectSingleNode(elemName);
            if (elem != null)
            {
                return elem.InnerText.Trim();
            }

            return null;
        }
}

Related Tutorials