开发者

C# Read in multiple profiles from streamreader and give outputs

开发者 https://www.devze.com 2023-03-10 23:48 出处:网络
I have been working on a program that goes along with a game me and some of my friends play, all I have done so far is make the interface, figured I would ask for help in advance because I know ill ne

I have been working on a program that goes along with a game me and some of my friends play, all I have done so far is make the interface, figured I would ask for help in advance because I know ill need it. I wanted to make a GUI that would read in data of a character from a text file, fill text boxes with it and be able to do equations with two of the boxes, then move onto another character from that text file by hitting next. sample read in data would be : Interface

   Hark  <- new character
   5
   2
   6
   40.0
   12.00
   Caro <- new character
   6
   1
   8
   38.0
   10.00
   Ethan <- new character
   4
   5
   42.0
   15.00

(The above is not actually code, just how it posted) So "name" through "char num" would just be populated by data.Readline(); from streamreader, but HP and armor would need to be times-ed together to get ratio (hitting the compute ratio button), all data from name to ratio would go in the t开发者_开发问答ext box below the buttons, the next button would cycle to the next character. Any help would be much appreciated, thanks in advance.


It's going to be a lot easier to do it with XML serialization/deserialization.

Here's a complete demo. It creates a list of two characters, serializes it to XML, then deserializes it back into a new List. This will take care of the storage aspect of it, at least.

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace XmlDemo
{

public class CharacterAttributes
{
    public string Name { get; set; }
    public int Strength { get; set; }
    public int Dexterity { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var characters = new List<CharacterAttributes>
                             {
                                 new CharacterAttributes
                                     {
                                         Name = "Throgdor the Destroyer",
                                         Strength = 5,
                                         Dexterity = 10
                                     }, 
                                new CharacterAttributes
                                    {
                                          Name = "Captain Awesome",
                                          Strength = 100,
                                          Dexterity = 9
                                    }
                             };
        SerializeToXML(characters);
        var charactersReloaded = DeserializeFromXML(@"C:\temp\characters.xml");

    }

    static public void SerializeToXML(List<CharacterAttributes> characters)
    {
        var serializer = new XmlSerializer(typeof(List<CharacterAttributes>));
        var textWriter = new StreamWriter(@"C:\temp\characters.xml");
        using (textWriter)
        {
            serializer.Serialize(textWriter, characters);
            textWriter.Close();                
        } 

    }

    public static List<CharacterAttributes> DeserializeFromXML(string path)
    {
        var serializer = new XmlSerializer(typeof(List<CharacterAttributes>));
        var textReader = new StreamReader(path);
        var deserializedCharacters = new List<CharacterAttributes>();
        using (textReader)
        {
            deserializedCharacters = serializer.Deserialize(textReader) as List<CharacterAttributes>;    
        }
        return deserializedCharacters;
    }

}
}


I would probably do a File.ReadAllText ( or something like that) into a string. That way you release the file handle. Then you can loop over the characters in the string that you read from the file and do whatever you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消