Create a new XML Element - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Create a new XML Element

Demo Code


using System.Xml;
using System;//from ww w . j  a v  a 2s. c  o  m

public class Main{
        /// <summary>
        /// Create a new XML Element
        /// </summary>
        /// <param name="parent">The parent node</param>
        /// <param name="name">The new element name</param>
        /// <param name="value">The new element value</param>
        /// <returns></returns>
        public static XmlElement NewElement(this XmlElement parent, string name, object value)
        {
            var e = parent.OwnerDocument.CreateElement(name);
            if (value != null)
                e.InnerText = value.ToString();
            parent.AppendChild(e);
            return e;
        }
        /// <summary>
        /// Create a new XML Element
        /// </summary>
        /// <param name="parent">The parent node</param>
        /// <param name="name">The new element name</param>
        /// <returns></returns>
        public static XmlElement NewElement(this XmlElement parent, string name)
        {
            return NewElement(parent, name, null);
        }
}

Related Tutorials