c# how to change a variable value with one from a text file

Updated on February 13, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on February 13, 2018

Hi, how can i read from a text file that have the name of variables and the value for each of them and update my current variables value to the ones that are in the file. 

For example:

In C# I have  int weight= 70;

                     int height=180;

In .txt file i have weight= 80

                         height=190

and so on a list of 15 variables…

How can i change the value of the weight from c# with one from .txt ?

  • Liked by
Reply
0 on February 13, 2018

You can create an xml file in your Resources folder, and structure it like this:

<Attributes>
     <Attribute name=”xx”>
           <help>some text here</help>
           <path>some value here</path>
      </Attribute >
     <Attribute name=”yyyy”>
        <help>other txt</help>
       <path>other value</path>
    </Attribute >

</Attributes>

 

and read it like this 

TextAsset txt = Resources.Load<TextAsset>(“yourXMLname”);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(txt.text);
XmlNodeList attrs= xmlDoc.GetElementsByTagName(“Spell”);
foreach(XmlNode att in attrs)
{
     //do stuff for each node inside the single attributes
}

 

 

check this xml thing out, see if that help!

  • Liked by
Reply
Cancel