12 Replies - 715 Views - Last Post: 07 November 2012 - 09:19 AM Rate Topic: -----

#1 gregwestwood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-June 12

Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 06:15 AM

Increase numbers within a line of text by a certain number.
Posted Today, 04:20 AM

Hi,

I want to start work on a programme that enables me to edit the below peice of text within an xml file.

<AREA SHAPE=rect COORDS="419,2049,551,2088" HREF="54043" >

1. I want to increase each of the above co-ordinates by 5.

2. In a different scenario I want to INCREASE co-ordinate 419 and 551 by 20

3. In a different scenario I want to DECREASE co-ordinate 2049 and 2088 by 20


The Co-ordinates will vary between file to file, but will always have 4 on account of it being a rectangle.


Any help and pointers to begin would be helpful. There's no code yet, I plan to start later. I just really want to know if this is even possible?

Is This A Good Question/Topic? 0
  • +

Replies To: Increase numbers within a line of text by a certain number.

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 06:19 AM

it should be possible, they are just XML Attributes.
Was This Post Helpful? 1
  • +
  • -

#3 gregwestwood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-June 12

Re: Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 06:35 AM

I was thinking I was going to have to put them in a listbox, seperate into columns,

insert into a sql database, edit and then export.

But I'm hoping for a quicker more efficient method.
Was This Post Helpful? 0
  • +
  • -

#4 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 06:38 AM

look into reading and modifying XML data
Was This Post Helpful? 1
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 06:38 AM

All that is really not necessary. You can do it all from within C# using the built in XML classes -

Link

This post has been edited by Ryano121: 27 June 2012 - 06:38 AM

Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 06:45 AM

Holy hell, don't do that. That's like putting an image into word so you can print it to scan it and then email the scan.

Why would you need to do any of that? You can do this all in memory. The first step is to read the XML. There are a variety of tools in C# that you can use for XML parsing. I like the tools in System.Xml.Linq namespace, but that's just me. Anyhow, there are literally tens of thousands of tutorials for "C# read XML" out there, so I'll leave that to you.

Once you've read in the XML, found your node, and grabbed that attribute value, you're left with a string that equals "419,2049,551,2088". And that's something you can deal with.

There's a method called string.Split. You can use that to split on commas, and then you have an array with four values. There's another method called int.Parse which will convert a string into an integer.

Now you can have four integers, which you can easily manipulate however you need.

Then you just go backwards. Convert each int to a string with .ToString(), recompose them into a single string using concatenation, and put them back in the XML file.
Was This Post Helpful? 2
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Increase numbers within a line of text by a certain number.

Posted 27 June 2012 - 09:05 AM

View PostCurtis Rutland, on 27 June 2012 - 06:45 AM, said:

Holy hell, don't do that. That's like putting an image into word so you can print it to scan it and then email the scan.

Totally unrelated to the topic but this reminded me of when I worked for an airline. This was back when you had physical tickets that were collected by the gate agents. These were then sent to us where we'd run them through a card reader and create files of the tickets then process them. Now along comes the idea of electronic tickets. So how do they solve the problem of integrating the electronic ticket files into the current system? By printing 'fake' tickets to be fed into the card reader. No, really, this was the solution.
Was This Post Helpful? 0
  • +
  • -

#8 gregwestwood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-June 12

Re: Increase numbers within a line of text by a certain number.

Posted 11 July 2012 - 01:20 PM

Hi,

Thanks everyone for your feedback. I have been having a really good go at this. I have got to the point whereby I can manipulate my 4 integers to do whatever I like and it's brilliant. However I just need one bit of help to finish this off.

I have multiple lines in the xml file and it only edits the top one. I am trying to use a foreach loop but I am struggling and wondered if you could help me accelerate the process?


<?xml version="1.0" encoding="utf-8"?>
<hotspot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hotspot.xsd">
<map name="40835804">
<area shape="rect" coords="1162,297,1219,329" href="120" />
<area shape="rect" coords="55,317,113,349" href="123" />
<area shape="rect" coords="942,500,994,531" href="121" />
<area shape="rect" coords="1268,716,1325,747" href="105" />
<area shape="rect" coords="820,722,877,753" href="114" />
</map>
</hotspot>
[/color][/xml][/size]
[/color]


using System.Xml;

namespace WindowsFormsApplication1
{

   
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

             
        }
        
         
        private void button1_Click(object sender, EventArgs e)
        {
            // Put the path of the xml file into a a textbox
             this.openFileDialog1.Filter =
               "xml (*.xml;*.hml|";
            this.openFileDialog1.Multiselect = false;
            this.openFileDialog1.Title = "My text editor";
           


            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)

                textBox1.Text = openFileDialog1.FileName;

        }

        private void button2_Click(object sender, EventArgs e)
        {

            
         try

         {
                //load the xml
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(textBox1.Text);

                    //choose the part of xml to read
                    XmlNode node = xmlDoc.SelectSingleNode("hotspot/map/area");

                    //extract the coords out as a string
                    string newValue = node.Attributes["coords"].Value;

                    //turn coords into a string array

                    string[] coordsarray = newValue.Split(',');



                    //convert to an integer array
                    int[] coordsarray2 = new int[coordsarray.Length];

                    for (int i = 0; i < coordsarray.Length; i++)
                    {

                        coordsarray2[i] = Convert.ToInt16(coordsarray[i]);


                        // divide the coords by 2 put into doubles
                        double first = Convert.ToInt16(coordsarray2[0] * 0.5);
                        double second = Convert.ToInt16(coordsarray2[1] * 0.5);
                        double third = Convert.ToInt16(coordsarray2[2] * 0.5);
                        double fourth = Convert.ToInt16(coordsarray2[3] * 0.5);

                        // choose where you wish to insert the data
                        XmlNode nodereplaced = xmlDoc.SelectSingleNode("hotspot/map/area");

                        //Choose the values to insert.
                        node.Attributes["coords"].Value = first.ToString() + "," + second.ToString() + "," + third.ToString() + "," + fourth.ToString();

                        // save
                        xmlDoc.Save(textBox1.Text);



                    }
                }
            

            catch
            {
                // The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Error"

                );
            }
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Increase numbers within a line of text by a certain number.

Posted 11 July 2012 - 01:31 PM

It's because you are using SelectSingleNode(). If you used SelectNodes(), you would get back a set of nodes you can iterate over and apply the same operation.
Was This Post Helpful? 1
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4559
  • View blog
  • Posts: 7,980
  • Joined: 08-June 10

Re: Increase numbers within a line of text by a certain number.

Posted 11 July 2012 - 03:02 PM

I don't usually do this, but I just want to show why I like LINQ so much. Here's the same basic logic, in a very compressed but readable format. This is a console application, but I think you can see the power of LINQ here. All this does is multiply each coordinate by .5 and save it to a new file, but you could change that pretty easily.

using System;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;

namespace Example
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var doc = Xdocument.Load(@"c:\dev\hotspot.xml");
            var elements = doc.Descendants("area").ToList();
            elements.ForEach(UpdateNode);
            doc.Save(@"c:\dev\hotspot2.xml");
        }

        private static void UpdateNode(XElement element)
        {
            var coordsAttribute = element.Attribute("coords");
            if (coordsAttribute == null) return;
            //splits the values and parses them into a List<double>
            var coords = coordsAttribute.Value
                .Split(',')
                .Select(double.Parse)
                .ToList();
            //perform some operation on coords
            coords = coords.Select(x => x * .5).ToList();
            var coordsString = coords
                .Select(x => x.ToString(CultureInfo.InvariantCulture)) //converts to string
                .Aggregate((w, n) => w + "," + n); //aggregates the strings into a single string
            element.SetAttributeValue("coords", coordsString);
        }
    }
}


by the way, the "d" in "XDocument" should be capitalized, but the forum software is changing it.
Was This Post Helpful? 1
  • +
  • -

#11 gregwestwood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-June 12

Re: Increase numbers within a line of text by a certain number.

Posted 25 September 2012 - 06:12 AM

This is amazing but I want to make all the numbers into integers. I am struggling with this. Thanks.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Increase numbers within a line of text by a certain number.

Posted 25 September 2012 - 09:12 AM

What have you tried?

On first glance, it looks like it's just a matter of changing line 28 to use integer division instead of floating point division.
Was This Post Helpful? 1
  • +
  • -

#13 gregwestwood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-June 12

Re: Increase numbers within a line of text by a certain number.

Posted 07 November 2012 - 09:19 AM

View PostSkydiver, on 25 September 2012 - 09:12 AM, said:

What have you tried?

On first glance, it looks like it's just a matter of changing line 28 to use integer division instead of floating point division.


I have since sorted it out, though not using the linq way. Thanks for your response. It is very much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1