Reputation: 53
So its my first time working with XML documents and i need some help. I have this segment in my XML file:
<configuration>
<appSettings>
<add key="PhoneVersion" value="36.999.1" />
<add key="TabletVersion" value="36.999.1" />
<add key="DesktopVersion" value="36.999.1" />
</appSettings>
</configuration>
I am trying to read the Value of each line and increment the final digit by +1.
I am able to read the entire file however i just want to read the lines stated.
Any help??
Upvotes: 0
Views: 68
Reputation: 53
Use XElement
to load the xml file. Then you can iterate the descendant nodes of the <configuration>
node with the method Descendants()
.
Finally you can read the attributes of the <add>
nodes with Attribute()
.
Upvotes: 0
Reputation: 34431
Try using Xml Linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication51
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
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));
}
}
}
}
Upvotes: 1