public static void AddToNode(this XmlDocument csproj, string node, string value, string platform, string configuration) { var nodes = csproj.SelectNodes($"//*[local-name() = '{node}']"); foreach (XmlNode?mea in nodes) { if (mea == null || !IsNodeApplicable(mea, platform, configuration)) { continue; } if (mea.InnerText.Length > 0 && mea.InnerText[mea.InnerText.Length - 1] != ' ') { mea.InnerText += " "; } mea.InnerText += value; return; } // The project might not have this node, so create one of none was found. var propertyGroups = csproj.SelectNodes("//*[local-name() = 'PropertyGroup' and @Condition]").Cast <XmlNode>(); var propertyGroup = propertyGroups.FirstOrDefault(v => EvaluateCondition(v, platform, configuration)); if (propertyGroup == null) { propertyGroup = csproj.AddPropertyGroup(platform, configuration); } var newNode = csproj.CreateElement(node, csproj.GetNamespace()); newNode.InnerText = value; propertyGroup.AppendChild(newNode); }
public static void AddAdditionalDefines(this XmlDocument csproj, string value, string platform, string configuration) { var projnode = csproj.SelectNodes("//*[local-name() = 'PropertyGroup' and @Condition]/*[local-name() = 'DefineConstants']"); foreach (XmlNode?xmlnode in projnode) { if (xmlnode == null) { continue; } var parent = xmlnode.ParentNode; if (parent?.Attributes["Condition"] == null) { continue; } if (!IsNodeApplicable(parent, platform, configuration)) { continue; } if (string.IsNullOrEmpty(xmlnode.InnerText)) { xmlnode.InnerText = value; } else { xmlnode.InnerText += ";" + value; } return; } projnode = csproj.SelectNodes("//*[local-name() = 'PropertyGroup' and @Condition]"); foreach (XmlNode?xmlnode in projnode) { if (xmlnode?.Attributes["Condition"] == null) { continue; } if (!IsNodeApplicable(xmlnode, platform, configuration)) { continue; } var defines = csproj.CreateElement("DefineConstants", csproj.GetNamespace()); defines.InnerText = "$(DefineConstants);" + value; xmlnode.AppendChild(defines); return; } var newPropertyGroup = csproj.AddPropertyGroup(platform, configuration); var defineConstantsElement = csproj.CreateElement("DefineConstants", csproj.GetNamespace()); defineConstantsElement.InnerText = "$(DefineConstants);" + value; newPropertyGroup.AppendChild(defineConstantsElement); }