0

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??

Zypherr
  • 53
  • 10

2 Answers2

1

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));
            }

        }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 1
    _I am able to read the entire file however i just want to read the lines stated_ - but `XDoxument.Load(FILENAME)` will read entire file. – Fabio Jul 13 '18 at 09:09
  • Windows doesn't allow you to modify a portion of the file. You have to read the entire file and then write it back. – jdweng Jul 13 '18 at 10:12
0

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().

Gilles-Antoine Nys
  • 1,481
  • 16
  • 21
CJ Thiele
  • 43
  • 8