/// <summary>
        /// Finds out if there are any validation errors in the edited node
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static List<string> ValidateTreeNode(XmlTreeNode node)
        {
            List<string> errors = new List<string>();

            if (XmlHelper.IsStringLegalXmlName(node.Name) == false)
                errors.Add("Invalid XML name. XML names are not allowed to contain \":\" or whitespace. ");

            if (node.Type == XmlNodeType.Namespace && XmlHelper.IsStringLegalNamespaceUri(node.Name) == false)
            {
                errors.Add("Namespace declarations must have a value.");
            }

            HttpContext.Current.Session[VALIDATION_ERRORS] = errors;

            return errors;
        }
 public ActionResult UpdateNode(XmlTreeNode updatedNode)
 {
     // A small hack because of a bug in the DevExpress TreeView. We call ValidateTreeNode to determine if the updatedNode contains invalid data
     List<string> errors = XmlTreeNodeValidator.ValidateTreeNode(updatedNode);
     // These errors are then added to the ModelState
     foreach (string error in errors)
     {
         ModelState.AddModelError(error, "");
     }
     // If no errors exist, we perform the update
     if (ModelState.IsValid)
     {
         XmlDataProvider.UpdateXmlData(updatedNode);
     }
     return PartialView("XmlTreePartial", ViewModelGenerator.GetXmlTreeModel());
 }
 private static void UpdateNamespaceDeclaration(XmlTreeNode updatedNode, NamespaceManager namespaceManager, XDocument doc, XmlTreeNode oldNode)
 {
     XAttribute attribute = (XAttribute)oldNode.XObject;
     namespaceManager.ChangeNamespaceDeclaration(doc, attribute, oldNode.Parrent, updatedNode.Value, updatedNode.Name);
 }
        private static void UpdateElement(XmlTreeNode updatedNode, NamespaceManager namespaceManager, XmlTreeNode oldNode)
        {
            XElement element = (XElement)oldNode.XObject;

            XmlHelper.ChangeLocalNameForElement(element, updatedNode.Name);

            // If the updated node has been assigned a namespace tag
            if (updatedNode.Tag != null)
            {
                XmlNamespace newNamespace = namespaceManager.GetNamespaceByPrefix(updatedNode.Tag);
                XmlHelper.ChangeNamespace(element, newNamespace);
            }

            // Parents don't have values, because XDocument does not like that
            if (oldNode.IsParrent == false)
                element.Value = updatedNode.Value;
        }
 private static void UpdateAttribute(XmlTreeNode updatedNode, XmlTreeNode oldNode)
 {
     // XAttribute does not allow us to change its name. Therefor we have to replace th old attribute with a new one
     XAttribute attribute = (XAttribute)oldNode.XObject;
     XAttribute newAttribute = XmlHelper.CreateAttribute(XmlHelper.ChangeLocalNameForAttribute(attribute, updatedNode.Name), updatedNode.Value);
     XmlHelper.ReplaceAttribute(attribute, newAttribute, oldNode.Parrent);
 }
        /// <summary>
        /// Updates the data for a specific XMl node
        /// </summary>
        /// <param name="updatedNode"></param>
        public void UpdateXmlData(XmlTreeNode updatedNode)
        {
            // If the new value of the updatedNode is null, the user wrote an empty value. We set the value of the updatedNode to empty string, since XDocument does not allow
            if (updatedNode.Value == null)
                updatedNode.Value = "";
            // The updated node that is sent back from the view does not contain an XObject. So we have to retrieve the node from the session
            List<XmlTreeNode> treeNodes = _dataSource.GetXmlTreeNodes();
            NamespaceManager namespaceManager = _dataSource.GetNamespaceManager();
            XDocument doc = GetXDocument();
            XmlTreeNode oldNode = treeNodes.Where(n => n.Id == updatedNode.Id).FirstOrDefault();

            if (oldNode.Type == XmlNodeType.Element)
            {
                UpdateElement(updatedNode, namespaceManager, oldNode);
            }
            else if (oldNode.Type == XmlNodeType.Attribute)
            {
                UpdateAttribute(updatedNode, oldNode);
            }
            else if (oldNode.Type == XmlNodeType.Namespace)
            {
                UpdateNamespaceDeclaration(updatedNode, namespaceManager, doc, oldNode);
            }
        }