I’m having difficulty reading an XML file and modifying it

Asked

Viewed 15 times

-1

I am creating a routine that will automatically change host configuration files in C# However I’m not sure how to handle the file . config which is actually an xml:

XML (file . config)

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="JobServer3Camadas" value="false" />
        <add key="Port" value="1232" />
        <add key="ActionsPath" value="C:\Arquivos\Legado\12.1.32\Bin\;C:\Arquivos\Atual\12.1.32\Bin\" />
        <add key="LibPath" value="C:\Arquivos\Legado\12.1.32\Bin\" />
        <add key="EnableDynamicLocalization" value="false" />
        <add key="EnableCompression" value="false" />
        <add key="LocalizationLanguage" value="pt-BR" />
    </appSettings>
</configuration>

Code I developed with google

 string xmlFilePathHost = @$"C:\Arquivos\Legado\12.1.32\Bin\Host.exe.config";
    XmlDocument doc = new XmlDocument();
          doc.Load(xmlFilePathHost);
          XmlNodeList addConfigList = doc.SelectNodes(@"/configuration/appSettings/add");
    
          foreach(XmlNode item in addConfigList)
          {
            if(item["Key"] == "Port"){
               //alterar porta
}
          }

In this case I needed to change the "Port" and the "Libpath" in this XML but I don’t know

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.