/// <summary> /// Remove attirbute on Node /// </summary> /// <param name="root"></param> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attribute"></param> public void RemoveAttribute(XElement root, string group, string node, CsProjAttribute attribute) { var ns = root.Name.Namespace; // validation var elements = string.IsNullOrEmpty(attribute.Value) ? root.Elements(ns + group).Elements(ns + node) .Where(x => x.HasAttributes) .Where(x => x.Attributes().Any(xs => xs.Name == attribute.Name)) .ToArray() : root.Elements(ns + group).Elements(ns + node) .Where(x => x.HasAttributes) .Where(x => x.Attributes().Any(xs => xs.Name == attribute.Name && xs.Value == attribute.Value)) .ToArray(); if (!elements.Any()) { return; } // remove attribute foreach (var item in elements) { item.FirstAttribute.Remove(); } }
/// <summary> /// Check attirbute is exists or not on Group /// </summary> /// <param name="group"></param> /// <param name="attribute"></param> /// <returns></returns> public bool ExistsAttribute(string group, CsProjAttribute attribute) { if (!Initialized) { throw new Exception("Detected not yet initialized, please run Load() first."); } return(ExistsAttribute(Root, group, attribute)); }
/// <summary> /// Replace attirbute on Node /// </summary> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attribute"></param> /// <param name="pattern"></param> /// <param name="replacement"></param> /// <param name="option"></param> public void ReplaceAttribute(string group, string node, CsProjAttribute attribute, string pattern, string replacement, RegexOptions option = RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant) { if (!Initialized) { throw new Exception("Detected not yet initialized, please run Load() first."); } ReplaceAttribute(Root, group, node, attribute, pattern, replacement, option); }
/// <summary> /// Remove attirbute on Node /// </summary> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attribute"></param> public void RemoveAttribute(string group, string node, CsProjAttribute attribute) { if (!Initialized) { throw new Exception("Detected not yet initialized, please run Load() first."); } RemoveAttribute(Root, group, node, attribute); }
/// <summary> /// Set attribute on Node /// </summary> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attribute"></param> /// <param name="filterElement"></param> public void SetAttribute(string group, string node, CsProjAttribute attribute, Func <XElement, bool> filterElement) { if (!Initialized) { throw new Exception("Detected not yet initialized, please run Load() first."); } SetAttribute(Root, group, node, attribute, filterElement); }
/// <summary> /// Insert attribute on Node /// </summary> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attributes"></param> /// <param name="filterElement"></param> public void InsertAttribute(string group, string node, CsProjAttribute attributes, Func <XElement, bool> filterElement, bool allowDuplicate = false) { if (!Initialized) { throw new Exception("Detected not yet initialized, please run Load() first."); } InsertAttribute(Root, group, node, new[] { attributes }, Eol.ToEolString(), filterElement, allowDuplicate); }
/// <summary> /// Check attirbute is exists or not on Group /// </summary> /// <param name="root"></param> /// <param name="node"></param> /// <param name="attribute"></param> /// <returns></returns> public bool ExistsAttribute(XElement root, string group, CsProjAttribute attribute) { var ns = root.Name.Namespace; var attributeElements = root.Elements(ns + group).Where(x => x.HasAttributes); // validation var element = string.IsNullOrEmpty(attribute.Value) ? attributeElements.Where(x => x.Attributes().Any(xs => xs.Name == attribute.Name)) : attributeElements.Where(x => x.Attributes().Any(xs => xs.Name == attribute.Name && xs.Value == attribute.Value)); return(element.Any()); }
/// <summary> /// Set attribute on Node /// </summary> /// <param name="root"></param> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attribute"></param> /// <param name="filterElement"></param> public void SetAttribute(XElement root, string group, string node, CsProjAttribute attribute, Func <XElement, bool> filterElement) { var ns = root.Name.Namespace; // validation var elements = root.Elements(ns + group).Elements(ns + node).Where(filterElement); if (!elements.Any()) { return; } // set attribute foreach (var target in elements) { target.SetAttributeValue(attribute.Name, attribute.Value); } }
/// <summary> /// Replace attirbute on Node /// </summary> /// <param name="root"></param> /// <param name="group"></param> /// <param name="node"></param> /// <param name="attribute"></param> /// <param name="pattern"></param> /// <param name="replacement"></param> /// <param name="option"></param> public void ReplaceAttribute(XElement root, string group, string node, CsProjAttribute attribute, string pattern, string replacement, RegexOptions option = RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant) { var ns = root.Name.Namespace; // validation var elements = root.Elements(ns + group).Elements(ns + node) .Where(x => x.HasAttributes) .Where(x => x.Attributes().Any(xs => xs.Name == attribute.Name && xs.Value == attribute.Value)) .ToArray(); if (!elements.Any()) { return; } // replace attribute foreach (var item in elements) { var replaced = Regex.Replace(item.FirstAttribute.Name.LocalName, pattern, replacement, option); item.ReplaceAttributes(new XAttribute(replaced, item.FirstAttribute.Value)); } }