我想在数据库中调用文本并显示它。然后让用户编辑显示的文本,就好像他/她在文本编辑器中打开它一样。当然最后我想将编辑后的版本保存回数据库。
不幸的是,我的所有想法都是死路一条,所以最终我决定问你,专业人士和更有经验的程序员,帮助我,因为我真的只是一个初学者。
这是我的出发点:
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#文件
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;
}
}
}
被修改
我试过这个:
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++;
}
}
问题是当光标移动时,前一个字母消失了。
答案 0 :(得分:0)
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");
}
}
}
从下面的代码中替换您的函数(SelectMenuOption)。并添加一个方法替换为如下所示。它会工作。你可以肯定地改进代码,我只是提供了一个解决方案。希望它会有所帮助。