10 Replies - 916 Views - Last Post: 23 September 2015 - 12:35 PM Rate Topic: -----

#1 jmadp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-September 15

C# reading xml value to int?

Posted 23 September 2015 - 06:11 AM

Hello programmers,

I have a little question. I've tried to read an xml value and increment it by 1 but i noticed it sees the value as a string.
Can anyone help me out here? tried to google it but nothing made sense.
The code i've tried is:
public void BFight_Click(object sender, RoutedEventArgs e)
        {
            XElement xelement = XElement.Load(@"Data\Monsters\"+ CMobSelect.Text + ".xml");
            IEnumerable<XElement> root = xelement.Elements();
            foreach (var Monster in root)
            {
                Mainwindow.Var.MName = Monster.Element("MName").Value;
                Mainwindow.Var.MLife = Monster.Element("MLife").Value;
                Mainwindow.Var.MExp = Monster.Element("MExp").Value;
                Mainwindow.Var.MDmg = Monster.Element("MDmg").Value;
                Mainwindow.Var.MGold = Monster.Element("MGold").Value;
            }

            

            MessageBox.Show("You have selected: "+ Mainwindow.Var.Gold);
            Mainwindow.Var.Gold += 1;
            MessageBox.Show("You have selected: " + Mainwindow.Var.Gold);
        }
MessageBox.Show("You have selected: " + Mainwindow.Var.MGold);
            Mainwindow.Var.MGold += 1;
            MessageBox.Show("You have selected: " + Mainwindow.Var.MGold);
is just to test if it incremented correctly which sadly didn't worked.

The xml file is:
<?xml version="1.0" encoding="utf-8"?>
<root>
  <Monster>
    <MName>Cat</MName>
    <MLife>20</MLife>
    <MDmg>4</MDmg>
    <MGold>10</MGold>
    <MExp>7,5</MExp>
  </Monster>
</root>


If you need more data please ask.


Thanks in advance,
jmadp

Is This A Good Question/Topic? 0
  • +

Replies To: C# reading xml value to int?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,746
  • Joined: 05-May 12

Re: C# reading xml value to int?

Posted 23 September 2015 - 06:21 AM

If you look at the documentation, it clearly states that the Value property is a string. Why would you expect otherwise?

If you read the documentation further, it suggest using the explicit conversion operators:

Quote

If you want to get the value of an element but you are not sure that it exists, it is more convenient to use the explicit conversion operators, and assign the element to a nullable type such as string or Nullable<T> of Int32. If the element does not exist, the nullable type is set to null. By contrast, if you want to use this property, you must make sure that the Element method does not return null before you access this property.


It is these conversion operators that LINQ to XML uses.
Was This Post Helpful? 0
  • +
  • -

#3 jmadp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-September 15

Re: C# reading xml value to int?

Posted 23 September 2015 - 06:31 AM

Yes i know it is a string now after i seen it didn't worked.
But i have no clue how to make it an int. And google didn't really helped me this time.

or isn't it possible to save and load int values? if it is possible could u send me in the right direction please?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,746
  • Joined: 05-May 12

Re: C# reading xml value to int?

Posted 23 September 2015 - 06:40 AM

*sigh* I already gave you the keywords to search for "explicit conversion operators"... Here's the link for conversion to int: XElement Explicit Conversion (XElement to Int32)
Was This Post Helpful? 0
  • +
  • -

#5 jmadp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-September 15

Re: C# reading xml value to int?

Posted 23 September 2015 - 06:56 AM

Okay this is what i've tried with your link:
XElement doc = new XElement("MGold", Mainwindow.Var.MGold);
            int value = (int)doc;
            MessageBox.Show("Test " + value);
            value += 1;
            MessageBox.Show("Test " + value);


Still doesn't work tho and that link wasn't great didn't said how to even use that code :S
XElement root = new XElement("Root", 2147483647);
int value = (int)root;
works just doesn't show me how to read existence xml data...
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,746
  • Joined: 05-May 12

Re: C# reading xml value to int?

Posted 23 September 2015 - 07:02 AM

What do you mean by "still doesn't work"? What is showing up for the two message boxes?

That link was to show the core functionality. It extends naturally to when you load XML and get an XElement after loading vs. the sample there where an XElement is created on the fly.
Was This Post Helpful? 0
  • +
  • -

#7 jmadp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-September 15

Re: C# reading xml value to int?

Posted 23 September 2015 - 07:11 AM

first message will send test 2147483647 and the second time test -2147483648.

Oké but then how would i use it in my script? cause it doesn't say it on that site?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,746
  • Joined: 05-May 12

Re: C# reading xml value to int?

Posted 23 September 2015 - 07:43 AM

That's right. The first value is Int32.MaxValue, if you add 1 to it, it will rollover and become Int32.MinValue. Why do you think that is not working correctly?

Why would you expect the MSDN documentation know every possible way that a class and its members could be used? I'm working a simulator -- should I expect MSDN's XElement documentation to show me how to integrate that into my simulator?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# reading xml value to int?

Posted 23 September 2015 - 08:31 AM

I'm going to be really, really blunt here. You won't like what I have to say. But its honest feedback instead of blowing sunshine up your kilt.

Clearly you're trying to write a game as a way to learn C#. That never works and you really have no business trying to write a program at this point. You simply don't know how much you don't know. Almost everything in your code block is just plain wrong in one way or another. All you're going to accomplish here is frustrate yourself, waste a lot of your time and build a TON of bad habits that will keep you from being employable.

Let's examine what's wrong with what you're doing and how you're doing it.

  • This is a WPF application but you're handling Button.Click as if you were building a 1990's WinForms application.
  • Tight binding between the logic and the GUI instead of separating these.
  • WTF is up with the MessageBox useage instead of making a nice display? This is a WPF application after all.
  • Your button handler method is doing work, instead of calling a method that performs a specific task.
  • You've hard coded the path to your XML file.
  • You're micro-managing the XML file element by element instead of deserializing it into a Monster object.
  • Clearly you're not developing this in an OOP way.
  • You're mingling method purposes. You're displaying feedback to the user in the same method that is reading the XML file. Bad programmer: No donuts!
  • You're trying to architect an entire program (a game no less!) without a fraking clue of what you're doing or having learned any of the basic things you would have picked up if you had worked your way through a couple "Learn C# in 30 days" books.


Basically you haven't bothered trying to LEARN before jumping in and thinking you're going to architect/design. Stop. Just stop. Work on learning for a while. Pick up on trying to build from scratch no sooner than 6 months from now: And only if you really work hard on learning for those 6 months.



My standard beginner resources post - Updated June 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.


Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?

WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.




I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 jmadp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-September 15

Re: C# reading xml value to int?

Posted 23 September 2015 - 09:45 AM

Honest feedback is wanted aslong it's in a friendly way ofcourse.
And yes i'm indeed trying to learn it that way(also did it with autoit and it worked for me, as i'm not as smart to learn it the "right" way.(it's true).

I also know that i a newb in C# and other languages. But i still like doing it the way i do i still like to learn more tho, just not the way u recommend. Besides i try to learn it for fun and maybe handy ways like creating tools i could use at work. Not as in getting a job as programmer.



"1. This is a WPF application but you're handling Button.Click as if you were building a 1990's WinForms application."
This is because i never seen another way how to do it. Also i don't know what the difference is between this and something else since i don't know about it. so this is something they teach in 30days C# books?

"3. WTF is up with the MessageBox useage instead of making a nice display? This is a WPF application after all."
It's only to test if something works:P Fastest way i know how to test it.

"6. You're micro-managing the XML file element by element instead of deserializing it into a Monster object."
I'm making a xml file of every mob so i can easily add or remove monsters. (don't know of a easier way so)

"7. Clearly you're not developing this in an OOP way."
I have no clue what OOP means so i guess not :P

Meanly why i don't read 30days books is because i don't got the patience. Although i'm starting to think i might need to do it someday.

Anyways thanks again for your reply tlhIn`toq
and i will look up some C# wpf tutorials.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# reading xml value to int?

Posted 23 September 2015 - 12:35 PM

1 - Take a look at any intro to WPF book under "Commands"
3 - Rather than have 500 individual places where you call "MessageBox.Show" it would be better to call one Method of "DisplayTestMessage(String message)". Then you can disable the one method as needed without having to disable it in 500 places.
bool IsDisplayTestMessages = true;
// Anyplace in your code 500 times
DisplayTestMessage("This is a test");
//

/// To stop messages from displaying just set IsDisplayTestMessages to false
void DisplayTestMessage(string msg)
{
   if (IsDisplayTestMessages) MessageBox.Show(msg);
}


Several places where you say "I don't know another way"
That's exactly my point. You don't know how much you don't know. Of course you don't know another way because you don't want to read about better ways. Basically it sounds like you think better ways are just going to magically come to you. Where they're going to come from I have no idea. I can tell you right now: Better ideas... An understand of OOP... all the basic foundation concepts of coding in C#... They aren't going to come to you in a dream one night.

Pick up a book or don't; that's up to you. But it takes about 10,000 hours to learn something well. Going the way you are going without a plan or lesson or not wanting to read about anything... You can probably expect it to take 10 times that. The technology will be end-of-life by the time you become proficient at it with this "bang on the keyboard and see what happens" approach.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1