using System;
using System.IO;
using System.Xml;

namespace Task3
{

   class TextFileReader
   {
           
      static void Main(string[] args)
      {
         XmlTextReader reader = new XmlTextReader("my.xml");
         String strn, strsn;
                 while (reader.Read())
                 {
                    switch (reader.NodeType)
                    {
                       case XmlNodeType.Element: // The node is an element.
                          if (reader.HasAttributes)
                          {
                             //Console.WriteLine(reader.GetAttribute("reader.Name"));
                             strn = reader.GetAttribute(0);
                             strsn = reader.GetAttribute(1);
                             //Console.WriteLine(strn);
                             //Console.WriteLine(strsn);
                          }
                          break;


                    }

                 }
                 
         int counter = 0;
         string line;
         // Read the file and display it line by line.
         System.IO.StreamReader file = new System.IO.StreamReader("read_file.txt");
         string ch;
         //int i = 0;
         while ((line = file.ReadLine()) != null)
         {
           
            
            if (line.Substring(0, 1).Equals("%"))
            {
               int a = line.IndexOf('%');
               int b = line.LastIndexOf('%');
               ch = line.Substring(a+1, b-1);
               if (ch == "name")
               {
                  string test = line.Replace(ch, strn);
                  //string test = line.Replace(ch, "abc");
                  Console.WriteLine(test);
               }
               else if (ch == "sirname")
               {
                  string test = line.Replace(ch, strsn);
                  //string test = line.Replace(ch, "xyz");
                  Console.WriteLine(test);
               }

            }
            else
            {
               Console.WriteLine(line);
            }
            counter++;

         }

         file.Close();

         // Suspend the screen.
         Console.ReadLine();



      }




   }
}

this is my code to raed from a text file, and then replace the value in it from the xml file, but it is giving some error, that
"Use of unassigned local variable 'strn'"
"Use of unassigned local variable 'strsn'"
please can someone solve this, i don know wat to do

i even tried using global variables, but the i am not able to deal with the values

Recommended Answers

All 2 Replies

i even tried using global variables, but the i am not able to deal with the values

Hi

Try replacing

String strn, strsn;

With

string strn = "";
string strsn = "";

That should do the trick.

Hi

Try replacing

String strn, strsn;

With

string strn = "";
string strsn = "";

That should do the trick.

Thanks...this is working...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.