Hi,
In these days I needed to increase the value of a “frequency” attribute contained into a certain XML file. Unfortunately this attribute appears several times into several XML files, so I wrote a simple WPF application that does this job for me 😉

Click here to download the application.
Click here to download the sample XML File.

The following picture show the Application. Once you digit the number you want to add to all frequency attributes you should press Ok button and after that an Open File Dialog appears allowing you to choose the XML file. After choosing the file, all frequency attributes will be updated according to the delta value and a backup of the original file will be stored as “yourXMLfile.back_0” 😉

How does it works?
The file is opened using the .Net XmlDocument , after that the the application selects all nodes containing the “frequency” attribute via XmlNodeList. Finally all “frequency” attribute value is updated and the XML file is saved.

Here the code:

                    // Open document
                    filename = dlg.FileName;

                     if (!File.Exists(filename))
                        throw new Exception("Satellites file not found!");

                    // Create a backup copy of source file
                    int counter = 0;
                    while (File.Exists(filename + ".bak_" + counter))
                        counter++;

                    backupFileName = filename + ".bak_" + counter;
                    File.Copy(filename, filename + ".bak_" + counter);

                    XmlDocument doc = new XmlDocument();
                    doc.Load(backupFileName);

                    XmlNodeList nodeList = doc.SelectNodes("satellites/sat/transponder");                      

                    foreach (XmlNode transponderNode in nodeList)
                    {
                        foreach (XmlAttribute transponderAttribute in transponderNode.Attributes)
                        {
                            if (transponderAttribute.Name.Equals("frequency", StringComparison.InvariantCultureIgnoreCase))
                            {
                                long currentValue = long.Parse(transponderAttribute.Value);
                                long newValue = currentValue + delta;
                                
                                transponderAttribute.Value = newValue.ToString();
                            }
                        }
                    }

                    doc.Save(filename);

Here how the XML file looks like:

<satellites>
  <sat name="37.5W NSS 10/Telstar 11N" flags="0" position="-375">
    <transponder frequency="12651000" symbol_rate="4280000" polarization="0" fec_inner="3" system="0" modulation="1" onid="235" tsid="17185" />
    <transponder frequency="12665264" symbol_rate="4280383" polarization="0" fec_inner="3" system="0" modulation="1" onid="65535" tsid="1" />
    <transponder frequency="12697500" symbol_rate="3214359" polarization="0" fec_inner="3" system="0" modulation="1" onid="1" tsid="1" />
  </sat>
...
</satellites>
Advertisements