0
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
      ...
    <connectionStrings>
      ...
    </connectionStrings>
    <applicationSettings>
        <Settings>
            ...
            <setting name="ServerConfig" serializeAs="String">
                <value>STAGE</value>
            </setting>
            ...
        </Settings>
    </applicationSettings>
</configuration>

I have the above XML structure, and for testing purposes I'm trying to throw together an app that will change the ServerConfig value element to be a different string (STAGE, PRODUCTION, INTERNAL). I'm unsure how to navigate to and update that value.

Edit:

XmlDocument xml = new XmlDocument();
            xml.Load("doc.xml");

            foreach (XmlElement element in xml.SelectNodes("setting"))
            {
                foreach (XmlElement child in element)
                {
                    if (element.SelectSingleNode("value").InnerText == "STAGE")
                    {
                        MessageBox.Show(child.InnerText);
                    }
                }
            }

This is the code I've been trying to get to work, but can't seem to get the value. I want to be able to select the setting with the name attribute "ServerConfig" and change the value of the value element.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • What are you using to manipulate the XML structure? Linq to XML or something else? – Guvante Jan 26 '15 at 16:09
  • [XPath](https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx) is what you're probably looking for. – Phylogenesis Jan 26 '15 at 16:12
  • To update the `applicationSettings` section, see duplicate. To edit XML in general, try searching some more, it's been asked before. See for example [C# : Modify a xml node](http://stackoverflow.com/questions/9616163/c-sharp-modify-a-xml-node). – CodeCaster Jan 26 '15 at 16:13
  • Not exactly what you want, but here's what I've done to access something deeply nested iconDataFromXML = xmlDoc.Descendants() .FirstOrDefault(e => e.Name.LocalName == "g") .Descendants() .Descendants() .Where(e => e.Name.LocalName == "path") – Ralt Jan 26 '15 at 16:13

1 Answers1

0

I recommend using the Slow Cheetah Visual Studio extension. It lets you modify your app settings for each individual build definition using xml transforms.

You can find more information about Slow Cheetah here

pquest
  • 3,151
  • 3
  • 27
  • 40
  • It's a little app that I'm whipping together as a tester to automate doing this process. I already did it in autohotkey, but I'm going through the exercise of rewriting it in C#. – Joe Vallillo Jan 26 '15 at 16:33