示例#1
0
        /// <summary>
        /// Creates and returns XML element corresponding to the sepcified 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);
        }