How Do I change a String Varible into a int varible

Hey I making a Xml Reader program and part of it use #s that I have stored in the xml file. <random>23</random>. Well I made a program that gets the #s and counts it down. Well the problem is it keeps telling me that I need a int varible and I can't convert a string into a int. Any Ideas or something.
[315 byte] By [Mega_x] at [2007-12-24]
# 1

to convert a string to an int....try this:

int theResult = 0;

if (int.TryParse(StringHere, out theResult))

{

//process - the string variable was successfully converted to integer and is now stored in "theResult"

}

does this help? if not, tell us what problems you are having and any specific errors etc...

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2
ahmedilyas wrote:

to convert a string to an int....try this:

int theResult = 0;

if (int.TryParse(StringHere, out theResult))

{

//process - the string variable was successfully converted to integer and is now stored in "theResult"

}

does this help? if not, tell us what problems you are having and any specific errors etc...

Sorry I tried it, it woundent convert the xml string into a int (and the xml string is 19). Maybe this would work if I could make the xml file have some kind of int varible inside it, but how can I do that, I only know basic xml.

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 3
ahmedilyas wrote:

to convert a string to an int....try this:

int theResult = 0;

if (int.TryParse(StringHere, out theResult))

{

//process - the string variable was successfully converted to integer and is now stored in "theResult"

}

does this help? if not, tell us what problems you are having and any specific errors etc...

It didn't work it didn't turn the string into a int varible. I mean I put in the xml string <random>19</random. I think to make this work I need to make the xml file have a # varible and make the program read it. The only problem about this idea is I don't know how to make a int varible in a xml document I only know basic xml.

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 4

so when reading the Xml - how exactly are you reading it? Can you post some code? The way the Integer.Parse works is that you must have the correct string format/which has the values you want which are integer values. Example:

"12345" -> this will work

"abc12345" -> this will not work as there is "abc"

all characters in the string must be of digits - if this makes sense?

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 5

Do i need to have the "" between them. Cause the code that I'm using looks something like this <random>19</random>.

So are you saying I need to make the code look like this <random>"19"</random>

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 6

if your code has this:

<random>19</random>

this will not convert to integer because of the <random> tags. you need to get the inner/text property of the tag, so only get the 19 part of of it

how are you reading the xml? are you able to give some code on this? you only need the string, which only has the numbers, you wish to convert to integer.

Here is an example on reading an xml document:

http://www.perfectxml.com/articles/xml/csharp.asp

http://support.microsoft.com/kb/307548/EN-US/

http://www.c-sharpcorner.com/Tutorials/ReadWriteXMLTutMellli21.asp

hope one of these helps you in some way but would be cool if you can post some code on how you are reading the Xml so we can help you better :-)

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 7

I think I might know how to make an integer but I don't think I know how to make the program read it:

<random varible="10"></random>

Is that what you ment

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 8

I think you maybe confusing yourself :-)

I've given the code on how to convert a string to an integer.

All that needs to be done is to obtain the value, the number (19 in the example). To do this, you need to read the node/element (<random>) to get the value in the tags/that's been set in the tag (<random>)

Again, can you show us the code you are using to read the xml file? :-)

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 9

System.Xml.XmlNode countdown = TvfeedItems.Item(0).SelectSingleNode("Play");

times.Interval = countdown.InnerText;

times.Enabled = true;

Thats the code I do have more at the top that loads the xml file and everything, but I think this is the main part you wana look at

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 10

probably is best to post the way you write the xml file (in its fullest) and how you read it. Here is a short but quick example on how I write/read to xml for this example:

write:



XmlTextReader theWriter = new XmlTextWrite(Application.StartupPath + "\\text.xml",null);
theWriter.WriteStartDocument();
theWriter.WriteStartElement("random");
theWriter.WriteString("123");
theWriter.WriteEndElement();
theWriter.WriteEndDocument();
theWriter.Close();

the result being when you open the xml file, itll look something like this:

<?xml version="1.0"?><random>123</random>

Similar to your example right? (except the Xml doc declaration)

so reading:



XmlTextReader theReader = new XmlTextReader(Application.StartupPath + "\\test.xml");
while (theReader.Read())
{
if (theReader.NodeType == XmlNodeType.Element)
{
MessageBox.Show("Element name: " + theReader.Name + Environment.NewLine + "Value of string: " + theReader.ReadString());
}
}
theReader.Close();

Remember, this isnt the only way to read and write to an xml file but for simplicity, I did this.

Now, when reading, it will give me a messagebox showing "Element name: Random Value of string: 123"

that's cool, thats what we want - the 123 part and the way to get that is by the "ReadString()" method of the textReader.

once we got that, then do the whole conversion as stated in my initial reply to convert the string read (123) to an integer.

does this help further or make it confusing? :-)

Again if possible, post the entire code on how you are writing, and reading, the xml file. Would be a big help to us all

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 11

Assuming that the string only contains one set of numbers you could:

string sNum = "<random>38</random>" ;

sNum = System.Text.RegularExpresision.Regex.Replace(sNum, @"\d+", "") ;

int iNum = Convert.ToInt32(sNum) ;

Mejin at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 12

you could but its not really recommended as its the incorrect way of reading the xml - you shouldnt need to use things that aren't needed if it can already be done within the Xml classes :-)

whilst the solution is valid, it is just a bit overload/overhead. As they say, there is always more than 1 solution to a problem

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 13
ahmedilyas wrote:

probably is best to post the way you write the xml file (in its fullest) and how you read it. Here is a short but quick example on how I write/read to xml for this example:

write:



XmlTextReader theWriter = new XmlTextWrite(Application.StartupPath + "\\text.xml",null);
theWriter.WriteStartDocument();
theWriter.WriteStartElement("random");
theWriter.WriteString("123");
theWriter.WriteEndElement();
theWriter.WriteEndDocument();
theWriter.Close();

the result being when you open the xml file, itll look something like this:

<?xml version="1.0"?><random>123</random>

Similar to your example right? (except the Xml doc declaration)

so reading:



XmlTextReader theReader = new XmlTextReader(Application.StartupPath + "\\test.xml");
while (theReader.Read())
{
if (theReader.NodeType == XmlNodeType.Element)
{
MessageBox.Show("Element name: " + theReader.Name + Environment.NewLine + "Value of string: " + theReader.ReadString());
}
}
theReader.Close();

Remember, this isnt the only way to read and write to an xml file but for simplicity, I did this.

Now, when reading, it will give me a messagebox showing "Element name: Random Value of string: 123"

that's cool, thats what we want - the 123 part and the way to get that is by the "ReadString()" method of the textReader.

once we got that, then do the whole conversion as stated in my initial reply to convert the string read (123) to an integer.

does this help further or make it confusing? :-)

Again if possible, post the entire code on how you are writing, and reading, the xml file. Would be a big help to us all

This looks like it might work. Thanks on the part on how to make a program write an xml file. I learned how to make a program do simple xml reading but now I know how to make a xml maker. Thanks! This program show work though, to understand it more I'm going to take a better look at the links you posted.

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 14

I know you told me how to convert a string into a int. I was just woundering how do I make a varible in a xml, and how do I read a varible in a xml file?

When I say write I don't mean program a program to make xml varibles but program it my self.

Just a small question

Mega_x at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...