Given an XML element, remove the closing operator for it, so you can add new child elements to it by concatenation. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Given an XML element, remove the closing operator for it, so you can add new child elements to it by concatenation.

Demo Code

//     Copyright (c) Microsoft Corporation.  All rights reserved.
using System.Text;
using System.Diagnostics;
using System;/* www . java2 s .  co m*/

public class Main{
        /// <summary>
        /// Given an XML element, remove the closing operator for it, so you can add new child elements to it by concatenation. 
        /// </summary>
        public static string OpenXmlElement(string xmlElement)
        {
            if (xmlElement.EndsWith("/>"))
                return xmlElement.Substring(0, xmlElement.Length - 2) + ">";
            int endTagIndex = xmlElement.LastIndexOf("</");
            Debug.Assert(endTagIndex > 0);
            while (endTagIndex > 0 && Char.IsWhiteSpace(xmlElement[endTagIndex - 1]))
                --endTagIndex;
            return xmlElement.Substring(0, endTagIndex);
        }
}

Related Tutorials