/// <summary> /// Conditionally removes a node from a document at specified node path. /// If <paramref name="comparedNode" /> is provided, the node also has to /// be equal to this to be removed. /// </summary> /// <param name="doc">The document to remove from</param> /// <param name="nodePath">The path of the node to remove</param> /// <param name="comparedNode">Node to compare with before removing. Can be null.</param> /// <returns>True if node was removed, false if not found or not equal to comparedNode.</returns> public static bool TryRemove(this XDocument doc, XmlNodePath nodePath, XNode comparedNode = null) { XNode removed; nodePath.TryFind(doc, out removed); if (removed == null || comparedNode != null && !comparedNode.DeepEquals(removed)) { return(false); } removed.Remove(); return(true); }
internal static bool TryAddAttribute(XDocument doc, XmlNodePath elementPath, XName key, string value) { XElement element; if (!elementPath.TryFind(doc, out element)) { return(false); } // Fail if attribute already exists if (element.Attribute(key) != null) { return(false); } // TODO: Think about attribute index? element.Add(new XAttribute(key, value)); return(true); }
internal static bool TryRemoveAttribute(XDocument doc, XmlNodePath elementPath, XName key, string value) { XElement element; if (!elementPath.TryFind(doc, out element)) { return(false); } // Fail if attribute already exists var attribute = element.Attribute(key); if (attribute == null || attribute.Value != value) { return(false); } attribute.Remove(); // TODO: Think about attribute index? return(true); }