C# update deeply nested XML element value [duplicate] - c#

This question already has answers here:
C# applicationSettings: how to update app.config?
(4 answers)
Closed 8 years ago.
<?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.

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

Related

Reading a string from app.config file

My question isn't related to the connection string which is the most popular. The other only help I could find with this is someone not using the reference.
What I'm trying to do: I have a string stored in the app.config file, which looks like:
<applicationSettings>
<App_5.Properties.Settings>
<setting name="Location_Name" serializeAs="String">
<value>String I want</value>
</setting>
My code to pull the string is:
string Loc_Name = ConfigurationManager.AppSettings["Location_Name"];
I have the reference added, and used. When I run the program my return is always null.
You are storing it in the wrong place in the app.config. For single name/value pairs use the appSettings. Then your retrieval code in the question will work.
<configuration>
<appSettings>
<add key="Location_Name" value="String I want" />
</appSettings>
</configuration>
Code (repeated from question)
var value = ConfigurationManager.AppSettings["Location_Name"];

how to modify xml node using c#

have that xml file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<applicationSettings>
<AllSettings>
<setting name="setting1" serializeAs="String">
<value>myValue</value>
</setting>
</AllSettings>
</applicationSettings>
</configuration>
and i want to change the value of <value> into something else, i have tried a couple method but can't find attribute <value>
You can use XElement.ReplaceWith:
XDocument doc = XDocument.Load("data.xml");
XElement value = doc.Root.Descendants("value").SingleOrDefault();
value.ReplaceWith(new XElement("value", "newValue"));
doc.Save("data.xml");
or, as the other answer suggested, XElement.SetValue:
value.SetValue("newValue");
You can load your XML in a XDocument object (System.Xml.Linq namespace) and then change the value like this:
// load XML from string:
var xdoc = XDocument.Parse(xml);
// or load XML from file:
var xdoc = XDocument.Load("filename.xml");
// change value
xdoc.Root.Element("applicationSettings").Element("AllSettings").Element("setting").Element("value").SetValue("myNewValue");

Is it possible to overwrite class attributes using web.config in C#?

I have a class that is decorated with a custom attribute derived from System.Attribute.
I want to change one property of this attribute.
Is it possible without modifying the code? Web.config maybe?
If you don't refer to your WebConfig from your compiled code, you won't be able to change the compiled Code later via WebConfig.
I think what you are looking for is something like this:
var name = Settings.Default.Name;
You can change this value later in your app/web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- ... -->
<setting name="Name" serializeAs="String">
<value>Robert</value>
</setting>
<!-- ... -->
</configuration>

Reading custom machine.config elements using XmlDocument?

In machine.config file there are elements written there by 3rd party software so it looks like this:
<configuration>
<configSections>
...
</configSections>
...
<Custom>
<Level1> ...
</Level1>
<Level2> ...
</Level2>
<Level3>
<add key="key_text1" value="s1" />
<add key="key_text2" value="s2" />
<add key="key_text3" value="s3" />
</Level3>
</Custom>
</configuration>
I want to get e.g. a value ("s2") of "value" attribute where key="key_text2" from configuration/Custom/Level3 node. So far, I tried to open machine.config as an XML and work from there:
Configuration config = ConfigurationManager.OpenMachineConfiguration();
XmlDocument doc = new XmlDocument();
doc.LoadXml(config.FilePath);
however, I get XmlException "Data at the root level is invalid.". I also don't know how to use Configuration class methods directly to get this done. Any ideas would be appreciated.
Use RuntimeEnvironment.SystemConfigurationFile to get machine.config location:
XmlDocument doc = new XmlDocument();
doc.Load(RuntimeEnvironment.SystemConfigurationFile);
Also why not to use Linq to Xml?
XDocument xdoc = XDocument.Load(RuntimeEnvironment.SystemConfigurationFile);
var element = xdoc.XPathSelectElement("//Custom/Level3/add[#value='s2']");
if (element != null)
key = (string)element.Attribute("key");
Try using the Load() method instead of LoadXml()
doc.Load(config.FilePath);
I also sould suggest you have a look at XDocument instead of XmlDocument. LINQ will really be of use when getting that value from the config file.

Getting section filtering to xml attribute

I want to select SMS section block according to tip attribute of SMS xml.
Currently: ConfigurationManager.GetSection("Logger/Sms") works but is there any way to get section like ConfigurationManager.GetSection("Logger/Sms[#tip='VF']")?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Logger">
<section name="Sms" type="caSectionTest.LogHandler, caSectionTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</sectionGroup>
</configSections>
<Logger>
<Sms tip="Clickatell">
<icerik>Soğuk zincir uygulamasından gönderilen sms</icerik>
<telNo>9053123123123</telNo>
<api>3363050</api>
<user>pkUser</user>
<pass>passhm</pass>
</Sms>
<Sms tip="Vodafone">
<icerik>write something into sms</icerik>
<telNo>905123123123</telNo>
<originator>336123</originator>
<user>ctUser</user>
<pass>9Mdfpass</pass>
</Sms>
</Logger>
</configuration>
You've probably long since moved on, but I created an XPath lookup for xml XElement recently available here: https://github.com/ChuckSavage/XmlLib/ If you want to use jsobo's comment to get the information you want.
You would use it like:
XElement root = XElement.Load(file);
XElement sms = root.XPathElement("//Sms[#tip={0}]", "VF"); // or "//Sms[#tip='VF']"
By using it with string.Format() syntax, you pass the type to the XPath as well, if you wanted to do a DateTime check, etc. I also find it easier for variable injections as well, instead of "//Sms[#tip='" + variable + "']". XPathElement is just XPath().FirstOrDefault() to return a single element.

Categories

Resources