- Home /
C# and XML Question
So I am trying to just simply grab some data from an XML file and what I've been finding just isn't making much sense to me, and also it's telling me "var" does not exist in current context. This is the current issue not allowing me to see if what I have even works! But I'll post my code so far below, but please correct me in I'm wrong on this, but i can just upload my xml file as an assets correct?
If someone could just elaborate on what the code does I have so far and explain to me if I'm even on the right track or not that would be great and save me some time. Also the var issue is really holding me up.
C#
[XmlRoot("MyKeno")];
public class SetUp
{
[XmlArray("Numbers"),XmlArrayItem("Number")]
public Keno[] KenoNums;
var numStart = SetUp.Load(Path.Combine(Application.dataPath, "num.xml"));
}
XML
<?xml version=”1.0" encoding=”utf-8" ?>
<KenoNumbers>
<numbers num="1" />
<numbers num="4" />
<numbers num="32" />
<numbers num="54" />
<numbers num="77" />
<KenoNumbers/>
var numStart = SetUp.Load(Path.Combine(Application.dataPath, "num.xml"));
This line should be in a method. You can't put that directly in the class declaration.
EDIT: As pointed out by @Baste, that's not correct: your initialization is valid, the problem is that the type must be explicit.
Answer by Baste · May 19, 2015 at 11:58 PM
You can put method calls in the declaration of fields as long as those method calls can be executed before the object exists - which means that you're limited to static calls. So that's not quite correct, @hu90.
That being said, you can't use type inference (ie. var) in that context - every field has to have an explicit type. This won't compile:
public class Foo {
var myInt = 1;
}
So the answer is simple - give the numStart the proper type - which would be the return type of the Load call.
Thanks, my bad. The initialization contains no member access/call. Updated my comment.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# Utility Class 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to read data from a constantly updating xml server? 1 Answer
Why is Start() from my base class ignored in my sub-class? 4 Answers