Click here to Skip to main content
14,768,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I’m still getting into the whole automation stuff and there are still areas that I am unfamiliar with – Today it turns out to be XML editing.
I haven’t worked much with XML files so I’m struggling slightly with what I’m trying to do.

I need to edit the 3 versions lines in the XML file, incrementing the version number of "PhoneVersion", "TabletVersion" & "DesktopVersion" by 1.

See XML below

<configuration>
  <appSettings>
    <add key="PhoneVersion" value="36.999.1" />
    <add key="TabletVersion" value="36.999.1" />
    <add key="DesktopVersion" value="36.999.1" />
    <add key="ToolsEnabledKey" value="tokolosheQA" />
    <add key="ToolsURL" value="" />
    <add key="DbCommandTimeOut" value="60" />
  </appSettings>
</configuration>


I haven’t had much luck actually reading or accessing the individual elements/attributes.

What I have tried:

This is what i have tried

XDocument doc = XDocument.Load(webconfig);

foreach (XElement add in doc.Descendants("add"))
 {
    string[] values = add.Attribute("value").Value.Split(new char[] { '.' });
    values[values.Length - 1] = (int.Parse(values[values.Length - 1]) + 1).ToString();
    add.SetAttributeValue("value", string.Join(".", values));
 }
Posted
Updated 13-Jul-18 7:40am

Try this, assuming your XML is in _strXml:

using System.Xml;


XmlDocument oDom = new XmlDocument();
oDom.LoadXml(_strXml);

XmlNode oXmlNode = oDom.FirstChild; // node "configurartion"
oXmlNode = oXmlNode.FirstChild;     // node "appSettings"

foreach (XmlNode oNode in oXmlNode.ChildNodes)
{
    if (oNode.Attributes["key"].Value == "PhoneVersion")
        oNode.Attributes["value"].Value = "whatever";
    // ... and so long ...
}
   
v3
Comments
Member 11546646 13-Jul-18 8:22am
   
So this makes perfect sense! However i keep getting an error

"CMLException was unhandled - Data at the root level is invalid. Line 1, position 1."


string webconfig = BuildLocation + "\\Web.config";

XmlDocument oDom = new XmlDocument();
oDom.LoadXml(webconfig);

XmlNode oXmlNode = oDom.FirstChild;
oXmlNode = oXmlNode.FirstChild;


foreach (XmlNode oNode in oXmlNode.ChildNodes)
{
if (oNode.Attributes["key"].Value == "PhoneVersion")
oNode.Attributes["value"].Value = "36.999.0.01";

}
Xml file does not contain lines! Xml[^] content is a set of nodes!


Try this:
XDocument xdoc = XDocument.Load(webconfig);
var nodes = xdoc.Descendants("add")
	.Select(x=> new
	{
		NodeName = x.Name,
		Key = (string)x.Attribute("key").Value,
		Value = (string)x.Attribute("value").Value
	})
	.ToList();


Result:
NodeName  Key               Value
add       PhoneVersion      36.999.1 
add       TabletVersion     36.999.1 
add       DesktopVersion    36.999.1 
add       ToolsEnabledKey   tokolosheQA 
add       ToolsURL   
add       DbCommandTimeOut  60 


to select single node:
XElement singlenode = xdoc.Descendants("add")
	.Where(x=> (string)x.Attribute("key").Value == "DesktopVersion")
	.SingleOrDefault();

singlenode.Attribute("value").Value = "Bla bla bla";

//save changes
xdoc.Save(webconfig)


For further details about manipulating xml content, please see: XDocument Class (System.Xml.Linq)[^]
   
v3
SO AFTER TINKERING WITH SOME IDEAS YOU GAVE ME - THIS WORKED GUY!!

string newValue = string.Empty;
     XmlDocument xmlDoc = new XmlDocument();

     xmlDoc.Load(webconfig);

     XmlNode PhoneVersionNode =
     xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='PhoneVersion']");
     PhoneVersionNode.Attributes[1].Value = "newValue";


     xmlDoc.Save(webconfig);


THANKS FOR THE HELP
   
Use the "configuration manager" to manage "configuration settings":

ConfigurationManager.AppSettings Property (System.Configuration)[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900