[英]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 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 C#文件
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. 从下面的代码替换您的功能(SelectMenuOption)。 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.
希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.