-2

Here I am attaching code which I am using

 {
  //reading data from file   
  XmlTextReader reader = new XmlTextReader(FileLocation);
  string s1;
  while (reader.Read())
    {
      //checking whether node name is Esig ang storing Esig value
      if (xtr.NodeType==XmlNodeType.Element&&xtr.Name=="Esig")
       {
         s1=xtr.ReadElementString();                                   
        }
     }

   Console.WriteLine(s1);
  }
    
      
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
vinay
  • 1
  • 4
  • 1
    You need to write: `string s1 = "";` –  May 17 '21 at 15:26
  • 1
    Does this answer your question? [Use of unassigned local variable - if statements](https://stackoverflow.com/questions/10449635/use-of-unassigned-local-variable-if-statements) and [C# use of unassigned local variable inside an if loop](https://stackoverflow.com/questions/37809362/c-sharp-use-of-unassigned-local-variable-inside-an-if-loop) –  May 17 '21 at 15:26
  • C# has very specific rules about whether it considers a variable "definitely assigned" and in the code above, the variable `s1` is **not** "definitely assigned". After all, the `Read()` method might return `false` the very first time you call it, and even if it didn't, you might never find a node of type `Element`, and even if you did, you might never find a node with the name `"Esig"`. See duplicates, and many other questions on this site as well, which discuss **the exact error message you're asking about**. – Peter Duniho May 17 '21 at 19:09

1 Answers1

-1

Local variables aren't initialized. You have to manually initialize them.

Members are initialized, for example:

public class X
{
    private string _userName; // This WILL initialize to zero
    ...
}

But local variables are not:

public static void SomeMethod()
{
    string userName;  // This is not initialized and must be assigned before used.

    ...
}

Hence you need to initialize local variable first and then use it.

Your Code will be like

string s1 = string.Empty;

More details are here on Microsoft LINK