Question

I have a program that updates a config file. For example, the config file may contain:

<configuration>
  <userSettings>
    <setting name="phoneNumber" serializeAs="String">
      <value>123-456-7890</value>
    </setting>
  </userSettings>
</configuration>

To update this config file, I use the following:

XmlNode phoneNumberNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']");
phoneNumberNode.FirstChild.InnerText = this._cloudPublisherWebURL;

Now, during the update I want to update phoneNumber and address. Address may or may not be in the config file.

If SelectSingleNode is null, I would like to create a node with the given path and set its value.

XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings/setting[@name='address']");
if(addressNode == null)
{
  //..Create the node here
}

How can I create the node with value at the given path?

Était-ce utile?

La solution

XmlNode addressNode = theConfig.SelectSingleNode("configuration/userSettings");
XmlNode setting = addressNode.Item(0).SelectSingleNode("configuration/userSettings/setting[@name='phoneNumber']");

setting.SetAttribute("name", "address"); //this is to change the name attribute value into address
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top