Creates and returns XML element corresponding to the specified location in the given XML element. - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Creates and returns XML element corresponding to the specified location in the given XML element.

Demo Code


using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
using System;/*from  w  w w  .j  ava 2 s.c  o  m*/

public class Main{
        /// <summary>
        /// Creates and returns XML element corresponding to the specified location in the given XML element.
        /// </summary>
        /// <param name="baseElement">The XML element.</param>
        /// <param name="location">The location string.</param>
        /// <returns>XML element corresponding to the sepcified location created in the given XML element</returns>
        public static XElement CreateLocation(XElement baseElement, string location)
        {
            var locSteps = location.SplitPathNamespaceSafe();

            XElement currentLocation = baseElement;
            foreach (string loc in locSteps)
            {
                if (loc == ".")
                {
                    continue;
                }
                else if (loc == "..")
                {
                    currentLocation = currentLocation.Parent;
                    if (currentLocation == null)
                        break;
                }
                else
                {
                    XName curLocName = loc;
                    XElement newLoc;
                    if (curLocName.Namespace.IsEmpty())
                        newLoc = currentLocation.Element(curLocName);
                    else
                        newLoc = currentLocation.Element_NamespaceNeutral(curLocName);

                    if (newLoc == null)
                    {
                        var newElem = new XElement(curLocName.OverrideNsIfEmpty(currentLocation.Name.Namespace));
                        currentLocation.Add(newElem);
                        currentLocation = newElem;
                    }
                    else
                    {
                        currentLocation = newLoc;
                    }
                }
            }

            return currentLocation;
        }
        public static bool IsEmpty(this XNamespace self)
        {
            return self != null && !String.IsNullOrEmpty(self.NamespaceName.Trim());
        }
        public static XName OverrideNsIfEmpty(this XName self, XNamespace ns)
        {
            if (self.Namespace.IsEmpty())
                return self;
            else if (ns.IsEmpty())
                return ns + self.LocalName;
            else
                return self;
        }
        public static XElement Element_NamespaceNeutral(this XContainer parent, XName name)
        {
            return parent.Elements().FirstOrDefault(e => e.Name.LocalName == name.LocalName);
        }
}

Related Tutorials