12 Replies - 485 Views - Last Post: 07 November 2012 - 09:19 AM
#1
Increase numbers within a line of text by a certain number.
Posted 27 June 2012 - 06:15 AM
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?
Replies To: Increase numbers within a line of text by a certain number.
#2
Re: Increase numbers within a line of text by a certain number.
Posted 27 June 2012 - 06:19 AM
#3
Re: Increase numbers within a line of text by a certain number.
Posted 27 June 2012 - 06:35 AM
insert into a sql database, edit and then export.
But I'm hoping for a quicker more efficient method.
#4
Re: Increase numbers within a line of text by a certain number.
Posted 27 June 2012 - 06:38 AM
#6
Re: Increase numbers within a line of text by a certain number.
Posted 27 June 2012 - 06:45 AM
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.
#7
Re: Increase numbers within a line of text by a certain number.
Posted 27 June 2012 - 09:05 AM
Curtis Rutland, on 27 June 2012 - 06:45 AM, said:
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.
#8
Re: Increase numbers within a line of text by a certain number.
Posted 11 July 2012 - 01:20 PM
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"
);
}
}
}
}
#9
Re: Increase numbers within a line of text by a certain number.
Posted 11 July 2012 - 01:31 PM
#10
Re: Increase numbers within a line of text by a certain number.
Posted 11 July 2012 - 03:02 PM
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.
#11
Re: Increase numbers within a line of text by a certain number.
Posted 25 September 2012 - 06:12 AM
#12
Re: Increase numbers within a line of text by a certain number.
Posted 25 September 2012 - 09:12 AM
On first glance, it looks like it's just a matter of changing line 28 to use integer division instead of floating point division.
#13
Re: Increase numbers within a line of text by a certain number.
Posted 07 November 2012 - 09:19 AM
Skydiver, on 25 September 2012 - 09:12 AM, said:
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.
|
|

New Topic/Question
Reply



MultiQuote




|