Insert xml element in specific location

I have lots of visual studio c++ project files I would like to be able to Switch Address Sanitizer on and off for and its really slow doing it all by hand. I’m trying to write a go program that loops through all the xml elements in the vcxproj until its find the below section and then inserts like below. <EnableASAN>true</EnableASAN>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
    <ConfigurationType>Application</ConfigurationType>
    <CharacterSet>MultiByte</CharacterSet>
    <PlatformToolset>v143</PlatformToolset>
    <EnableASAN>true</EnableASAN>
  </PropertyGroup>

This is the code I have so far to find the right PropertyGroup but now I have no idea how to insert an xml element here. Can anyone point me in the right right direction how to do this if its possible.

I think you can try rhe vsproj as a plain text file and then look for the required token in the read line. For example:

package main

import (
“bufio”
“fmt”
“log”
“os”
“strings”
)

func printLines(filePath string, lines string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
for _, value := range lines {
fmt.Fprintln(f, value)
}
return nil
}

func main() {
f, err := os.Open(“project1.vcxproj”)

if err != nil {
fmt.Println(err)
}
defer f.Close()
fileScanner := bufio.NewScanner(f)

fileScanner.Split(bufio.ScanLines)

lines := make(string, 0)
found := false

for fileScanner.Scan() {
line := fileScanner.Text()

  if strings.Contains(line, "PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\"") && !found {
  	found = true
  }

  if found && strings.Contains(line, "</PropertyGroup>") {
  	lines = append(lines, "<EnableASAN>true</EnableASAN>")
  	found = false
  }

  lines = append(lines, line)

}

if err := printLines("project2.vcxproj", lines); err != nil {
    log.Fatal(err)
}
}

@YamilBrecho The issue I have is the EnableASAN element doesn’t exist in the xml when switched off so cant just text replace it. My plan for removing the element to switch this off was basically what you have done though so thanks for that.

@YamilBrecho thanks for the help with this, read your solution again and it does work just me being stupid

made some changes so it wouldn’t write the EnableASAN element again if present but does work thanks for that.

fileScanner := bufio.NewScanner(f)

	fileScanner.Split(bufio.ScanLines)
	lines := make([]string, 0)
	found := false
	asanTrue := false

	for fileScanner.Scan() {
		line := fileScanner.Text()
		if strings.Contains(line, "PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\"") && !found {
			found = true
		}
		if strings.Contains(line, "<EnableASAN>true</EnableASAN>") {
			asanTrue = true
		}

		if found && strings.Contains(line, "</PropertyGroup>") && !asanTrue {
			lines = append(lines, "<EnableASAN>true</EnableASAN>")
			found = false
		}

		lines = append(lines, line)

	}

	if err := printLines(s, lines); err != nil {
		log.Fatal(err)
	}

To insert an XML element in a specific location, you can use the following steps:

Parse the XML document into an object model, such as an XmlDocument or an XmlElement in .NET, or an ElementTree in Python.

Navigate to the parent node of the desired location using the object model’s navigation methods, such as SelectSingleNode , GetElementsByTagName , or find in Python.

Create a new element using the object model’s element creation method, such as CreateElement , Element , or SubElement in Python.

Set the attributes and text content of the new element as needed.

Insert the new element into the parent node using the object model’s insertion method, such as AppendChild , append , or insert in Python.

Save the XML document to a file or string if necessary.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.