界面见下:(加单词进字典是个枯燥活,所以允许用户自己回单词进字典)
开发时加单词进字典用的是索引器,发布后若没单词,用户加单词进字典用的是XML文件
用户加单词进字典(写XML文件)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sXmlFileName);
XmlNode root = xmlDoc.SelectSingleNode("UserWords");
XmlElement newWord = xmlDoc.CreateElement("word");
newWord.SetAttribute("English", textBox1.Text.Trim().ToLower());
newWord.SetAttribute("Chinese", richTextBox1.Text);
root.AppendChild(newWord);
xmlDoc.Save(sXmlFileName);
MessageBox.Show("提示:用户添加字典成功!");查单词时先查索引器,若没有,再查XML文件,若没有,提示没找到,用户可添加
(查XML文件)
XDocument xDoc = XDocument.Load(Application.StartupPath + @"\UserWords.xml");
var words = from word in xDoc.Descendants("word")
where word.Attribute("English").Value == textBox1.Text.Trim().ToLower()
select word.Attribute("Chinese").Value;
foreach (var word in words)
{
//有从XML文件找到
richTextBox1.AppendText(word);
}若用户想修改自己已添加进XML文件的单词(修改XML文件)
var updates = from updateWord in xDoc.Descendants("word")
where updateWord.Attribute("English").Value == textBox1.Text.Trim().ToLower()
select updateWord;
foreach (var update1 in updates)
{
update1.Attribute("Chinese").Value = richTextBox1.Text;
MessageBox.Show("提示:用户修改字典成功!")
}XML文件内容格式见下:
<?xml version="1.0"?>
<UserWords>
<word English="andy" Chinese="安迪" />
</UserWords>
开发时加单词进字典用的是索引器,发布后若没单词,用户加单词进字典用的是XML文件
用户加单词进字典(写XML文件)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sXmlFileName);
XmlNode root = xmlDoc.SelectSingleNode("UserWords");
XmlElement newWord = xmlDoc.CreateElement("word");
newWord.SetAttribute("English", textBox1.Text.Trim().ToLower());
newWord.SetAttribute("Chinese", richTextBox1.Text);
root.AppendChild(newWord);
xmlDoc.Save(sXmlFileName);
MessageBox.Show("提示:用户添加字典成功!");查单词时先查索引器,若没有,再查XML文件,若没有,提示没找到,用户可添加
(查XML文件)
XDocument xDoc = XDocument.Load(Application.StartupPath + @"\UserWords.xml");
var words = from word in xDoc.Descendants("word")
where word.Attribute("English").Value == textBox1.Text.Trim().ToLower()
select word.Attribute("Chinese").Value;
foreach (var word in words)
{
//有从XML文件找到
richTextBox1.AppendText(word);
}若用户想修改自己已添加进XML文件的单词(修改XML文件)
var updates = from updateWord in xDoc.Descendants("word")
where updateWord.Attribute("English").Value == textBox1.Text.Trim().ToLower()
select updateWord;
foreach (var update1 in updates)
{
update1.Attribute("Chinese").Value = richTextBox1.Text;
MessageBox.Show("提示:用户修改字典成功!")
}XML文件内容格式见下:
<?xml version="1.0"?>
<UserWords>
<word English="andy" Chinese="安迪" />
</UserWords>
用到的就是二技术:一是索引器,见这个贴子参考:
http://topic.csdn.net/u/20100827/22/02b76940-34d5-48f9-becc-159749e51c38.html二是对XML文件操作(查,修改,添加)