c# - 导入xml文件 [英] c# - importing xml file
问题描述
在我的项目中,我有一个列表视图,其中包含存储在XML文件中的联系人,这是两者之间的连接。基本上这是联系人的加载方式:
请注意第一个函数是在表单加载时调用的:
In my project I have got a listview filled with contacts which are stored in a XML file and this is the connection between the two of these. Basically this is how the contacts are loaded:
Please note that first function is called on form loading:
void LoadContacts()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string phonebook_path = path + "\\Phonebook\\Contacts.xml";
if (!File.Exists(phonebook_path))
{
XmlTextWriter xW = new XmlTextWriter(phonebook_path, Encoding.UTF8);
xW.WriteStartElement("People");
xW.WriteEndElement();
xW.Close();
}
XmlDocument xDoc = new XmlDocument();
xDoc.Load(phonebook_path);
foreach (XmlNode xNode in xDoc.SelectNodes("People/Person"))
{
Person p = new Person();
p.Name = xNode.SelectSingleNode("Name").InnerText;
p.Hometown = xNode.SelectSingleNode("Hometown").InnerText;
p.Address = xNode.SelectSingleNode("Address").InnerText;
p.Birthday = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
p.Phone = xNode.SelectSingleNode("Phone").InnerText;
p.Email = xNode.SelectSingleNode("Email").InnerText;
p.AdditionalInfo = xNode.SelectSingleNode("AdditionalInfo").InnerText;
people.Add(p);
listView1.Items.Add(p.Name);
UserCount();
}
}
然后另一个关闭表格:
and then the other one on form closing:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
XmlDocument xDoc = new XmlDocument();
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string phonebook_path = path + "\\Phonebook\\Contacts.xml";
xDoc.Load(phonebook_path);
XmlNode xNode = xDoc.SelectSingleNode("People");
xNode.RemoveAll();
foreach (Person p in people)
{
XmlNode xTop = xDoc.CreateElement("Person");
XmlNode xName = xDoc.CreateElement("Name");
XmlNode xHometown = xDoc.CreateElement("Hometown");
XmlNode xAddress = xDoc.CreateElement("Address");
XmlNode xBirthday = xDoc.CreateElement("Birthday");
XmlNode xPhone = xDoc.CreateElement("Phone");
XmlNode xEmail = xDoc.CreateElement("Email");
XmlNode xAdditionalInfo = xDoc.CreateElement("AdditionalInfo");
xName.InnerText = p.Name;
xHometown.InnerText = p.Hometown;
xAddress.InnerText = p.Address;
xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
xPhone.InnerText = p.Phone;
xEmail.InnerText = p.Email;
xAdditionalInfo.InnerText = p.AdditionalInfo;
xTop.AppendChild(xName);
xTop.AppendChild(xHometown);
xTop.AppendChild(xAddress);
xTop.AppendChild(xBirthday);
xTop.AppendChild(xPhone);
xTop.AppendChild(xEmail);
xTop.AppendChild(xAdditionalInfo);
xDoc.DocumentElement.AppendChild(xTop);
}
xDoc.Save(phonebook_path);
Sync();
SetStartup();
}
I have got an option to import the XML file via method Import() which is called when a button is clicked.
void Import()
{
Main f1 = new Main();
OpenFileDialog BrowseFile = new OpenFileDialog();
if (BrowseFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.ImportPath = BrowseFile.FileName;
}
string fileName = "Contacts.xml";
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string sourcePath = Properties.Settings.Default.ImportPath;
string targetPath = path + "\\Phonebook\\";
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourcePath, destFile, true);
MessageBox.Show("Contacts have successfully been imported. Please restart your application in order changes to take effect!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
...导入完美执行,这意味着我可以在正确的位置看到复制的XML文件,但是当我关闭应用程序时,所有的一个突然的XML文件以某种方式变成以前的状态,这意味着更改没有生效 - 当我再次加载应用程序时,将没有导入的联系人。
怎么能我解决了这个问题?
...Importing is performed perfectly which means I can see the copied XML file in the right place, but when I close the application, all of a sudden XML file turns into previous state somehow which means changes didn't take effect - when I load the app again, there will be no imported contacts.
How can I solve this issue?
推荐答案
你忘了保存Properties.Settings的新值:
You forgot to save the new value of the Properties.Settings:
Properties.Settings.Default.ImportPath = BrowseFile.FileName;
Properties.Settings.Default.Save();
如果你不这样做,信息不会在应用程序运行之间保存,所以它会获取旧的路径信息。
If you don't do that the info isn't saved between application runs, so it picks up the old path info.
更多访问这里..
如何在c#中导入XML数据 [ ^ ]
For more visit here..
How to import XML data in c#[^]
这就是我想出的:
This is what I came up with:
void Import()
{
XmlDocument xDoc = new XmlDocument();
OpenFileDialog BrowseFile = new OpenFileDialog();
if (BrowseFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.ImportPath = BrowseFile.FileName;
Properties.Settings.Default.Save();
}
string fileName = "Contacts.xml";
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string sourcePath = Properties.Settings.Default.ImportPath;
string targetPath = path + "\\Phonebook\\";
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourcePath, destFile, true);
xDoc.Save(destFile);
MessageBox.Show("Contacts have successfully been imported. Please restart your application in order changes to take effect!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
实际上在应用程序重启后加载了联系人,但问题是我在执行时遇到错误进口()。
这就是错误:
图片 [ ^ ]
因此,在此之后我关闭应用程序,再次运行它并导入联系人。
有什么想法吗?
It actually loads contacts after the app is restarted, but the problem is I am getting an error when I perform Import().
This is what the error is about:
IMAGE[^]
So, after that I close the application, run it again and the contacts are imported.
Any ideas?
这篇关于c# - 导入xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!