简体   繁体   中英

Problems creating a xml parser for windows phone in C#

I'm not an expert programmer, mostly self-trained. Currently my Project is to create a XML parser for an application im writing in C# for Windows phone. To learn that, I'm trying the demo posted here: http://www.developer.nokia.com/Community/Wiki/Parse_Local_XML_file_in_Windows_Phone

all is good until I get to the following part:

     private void btnparse_Click(object sender, RoutedEventArgs e) 
    {
             this._parser = XMLParser.Instance;        
             StreamResourceInfo strm = Application.GetResourceStream(new Uri("/LocalXmlParsing;component/States.xml",UriKind.Relative));
     //needs to be done only once
             StreamReader reader = new StreamReader(strm.Stream);
             string data = reader.ReadToEnd();
             _parser.DataToParse = data;
             _parser.ParseStateData();
             lstStates.ItemsSource = _parser.StateCollection; 
}

I get the Error: "Error 2 The name '_parser' does not exist in the current context" I will take any advice you guys can give me.

this is always a reference to the current object (whose member functions are executing against). So this._parser is called a "field" or "member variable". These fields have to be defined in the class definition. You're not showing us the full class definition, but it certainly doesn't exist. And that's what the compiler is complaining about.

It should look something like this:

class Foo {
    private XMLParser _parser;

    // your functions, like btnparse_Click
}

Or, if you're only going to use the parser in the context of that one function, just make it a local variable :

private void btnparse_Click(object sender, RoutedEventArgs e) 
{
    XMLParser parser = XMLParser.Instance;
    ...

You can also use an implicitly typed local variable :

private void btnparse_Click(object sender, RoutedEventArgs e) 
{
    var parser = XMLParser.Instance;
    ...

check if the _parser is defined as class variable Or if it is a variable in the parent class (in case your class has inherited a base class). I have not looked at the entire code but that is what the error suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM