Appends an element to XML node. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Appends an element to XML node.

Demo Code

// License:         MIT License (http://www.opensource.org/licenses/mit-license.php)
using System.IO;// w w w .  ja  v  a2 s . co m
using System.Text;
using System.Xml;
using System;

public class Main{
        /// <summary>
        /// Appends an element to XML node.
        /// </summary>
        /// <param name="document">Document object.</param>
        /// <param name="parentNode">Parent node for append.</param>
        /// <param name="elementName">Name of new element.</param>
        /// <param name="elementInnerText">Inner text of new element.</param>
        /// <returns></returns>
        static public XmlElement AppendElement(XmlDocument document, XmlNode parentNode, string elementName, string elementInnerText)
        {
            // If ParentNode isn't set then get root node
            if (null == parentNode)
                parentNode = document.DocumentElement;

            // If ParentNode still isn't set then exit
            if (null == parentNode)
                return null;

            // Create child element and return new node
            XmlElement element = document.CreateElement(elementName);
            element.InnerText = elementInnerText;
            XmlNode node = parentNode.AppendChild(element);
            return element;
        }
}

Related Tutorials