简体   繁体   中英

How can I display a text and then let the user edit it? (console application)

I would like to call the database for a text and display it. Then let the user edit the displayed text as if she/he opened it in a text editor. Of course at the end I would like to save the edited version back to the database.

Unfortunately all of my ideas were dead ends, so eventually I decided to ask you, professionals and more experienced programmers, to help me as I am really just a beginner.

Here is my starting point:

Database.xml

<?xml version="1.0" encoding="utf-8" ?> 
<Database>

  <Texts>

      <Text Id="00">Default text 00</Text>
      <Text Id="01">Default text 01</Text>
      <Text Id="02">Default text 02</Text>

  </Texts>

</Database>

C# file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace EditableText
{
    class Program
    {

        static void Main(string[] args)
        {

            SelectMenuOption();

            Console.WriteLine("Thanks for stopping by!");
            Console.ReadLine();

        }

        private static void SelectMenuOption()
        {
            bool tf = true;
            while (tf)
            {
                Menu();
                string userInput = Console.ReadLine().ToLower();
                Console.WriteLine("--------------------------------------------------");

                if (userInput == "text_00")
                {
                    Console.Write(CallDatabase("00"));
                    tf = false;
                }
                else if (userInput == "text_01")
                {
                    Console.Write(CallDatabase("01"));
                    tf = false;
                }
                else if (userInput == "text_02")
                {
                    Console.Write(CallDatabase("02"));
                    tf = false;
                }
                else if (userInput == "quit")
                {
                    tf = false;
                }
                else
                {
                    Console.WriteLine("Error");
                }

                Console.ReadLine();
                Console.Clear();
            }

        }

        private static void Menu()
        {

            Console.WriteLine("You may play or write quit to say bye.");
            Console.WriteLine("");

            string[] texts = new string[]
            {
                "Text_00",
                "Text_01",
                "Text_02"
            };

            Console.WriteLine("Choose: ");

            foreach (var text in texts)
            {
                Console.Write("   " + text);
            }

            Console.WriteLine("");

        }

        private static string CallDatabase(string idNumber)
        {

            XElement database = XElement.Load(@"Database.xml");
            string dText = database.Element("Texts").Elements("Text").Where(x => x.Attribute("Id").Value == idNumber).FirstOrDefault().Value.ToString();
            return dText;

        }

    }
}

Edited
I've tried this:

    string text = "It's a text.";

    char[] textC = text.ToCharArray();
    foreach (var textL in textC)
    {
        Console.Write(textL);
    }

    int n = 0;

    while (0 <= textC.Length - n)
    {
        if (Console.ReadKey().Key == ConsoleKey.LeftArrow)
        {
            Console.SetCursorPosition(textC.Length - n, 0);
            n++;
        }
    }

And the problem was that when the cursor moved, the previous letter disappeared.

private static void SelectMenuOption()
    {
        bool tf = true;
        while (tf)
        {
            Menu();
            string userInput = Console.ReadLine().ToLower();
            Console.WriteLine("--------------------------------------------------");

            string id = string.Empty;

            if (userInput == "text_00")
            {

                Console.Write(CallDatabase("00"));
                id = "00";
                tf = false;
            }
            else if (userInput == "text_01")
            {
                Console.Write(CallDatabase("01"));
                id = "01";
                tf = false;
            }
            else if (userInput == "text_02")
            {
                Console.Write(CallDatabase("02"));
                id = "02";
                tf = false;
            }
            else if (userInput == "quit")
            {
                tf = false;
            }
            else
            {
                Console.WriteLine("Error");
            }

            var chage  = Console.ReadLine();

            Replace(id, chage);

            Console.Clear();
        }

    }

    private static void Replace(string id, string chage)
    {

        XmlDocument xml = new XmlDocument();
        xml.Load(@"Database.xml");

        XmlNodeList elements = xml.SelectNodes("//Text");



        foreach (XmlNode element in elements)
        {
            if (element.Attributes["Id"].Value == id)
            {
                element.InnerText = chage;

                xml.Save("Database.xml");
            }
        }

    }

Replace your functions(SelectMenuOption) from the code below. and Add one more method Replace in it as below. It will work. You can improve the code for sure, I just provided a solution. Hope it will help.

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