我有一个程序可以输入一些关于人的信息。例如:
Surname: Tomson
Forename: Tyler
然后我点击“添加”按钮,它将新人添加到listView
。如果我现在重新启动程序,名称会更改。所以必须有“汤姆森泰勒”,但有“泰勒汤臣”。如果我再次重新启动它,它会再次更改名称。所以每次重启都会改变。我不知道为什么。以下是我将代码添加到listView
:
List<People> people = new List<People>();
private void btnAdd_Click(object sender, EventArgs e)
{
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!File.Exists(path + "\\save.xml"))
{
XmlTextWriter xwriter = new XmlTextWriter(path + "\\save.xml", Encoding.UTF8);
xwriter.WriteStartElement("Root");
xwriter.WriteEndElement();
xwriter.Close();
}
People p = new People();
p.Surname = txtSurname.Text;
p.Forename = txtForename.Text;
people.Add(p);
listViewKunden.Items.Add(p.Surname + " " + p.Forename);
txtSurname.Text = "";
txtForename.Text = "";
}
class People
{
public string Surname
{
get;
set;
}
public string Forename
{
get;
set;
}
}
编辑:
我忘了分享加载和保存xml
文件的代码。这里是。第一部分用于加载/读取,第二部分用于保存:
private void Form1_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument xdocument = new System.Xml.XmlDocument();
xdocument.Load(path + "\\save.xml");
foreach (XmlNode xnode in xdocument.SelectNodes("Root/People"))
{
People p = new People();
p.Nachname = xnode.SelectSingleNode("Surname").InnerText;
p.Vorname = xnode.SelectSingleNode("Forename").InnerText;
people.Add(p);
listViewKunden.Items.Add(p.Surname + " " + p.Forename);
}
using (FileStream rfs = new FileStream(path + "\\save.xml", FileMode.Open, FileAccess.Read))
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(rfs);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml("<Root></Root>");
XmlNode xnode = xdoc.SelectSingleNode("Root");
using(FileStream wfs = new FileStream(path + "\\NewFred.xml",FileMode.Create,FileAccess.Write))
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Save(wfs);
}
foreach (People p in people)
{
XmlNode xtop = xdoc.CreateElement("People");
XmlNode xsurname = xdoc.CreateElement("Surname");
XmlNode xvorname = xdoc.CreateElement("Forename");
xsurname.InnerText = p.Forename;
xforename.InnerText = p.Surname;
xtop.AppendChild(xsurname);
xtop.AppendChild(xforename);
xdoc.DocumentElement.AppendChild(xtop);
}
xdoc.Save(path + "\\save.xml");
e.Cancel = false;
}
答案 0 :(得分:1)
你刚刚切换了这些:
xsurname.InnerText = p.Forename;
xforename.InnerText = p.Surname;
他们应该是:
xsurname.InnerText = p.Surname;
xforename.InnerText = p.Forename;
答案 1 :(得分:0)
公司必须阅读有关XML序列化和反序列化的内容。 属性和类模型中的第二个mising属性。
var serializer = new XmlSerializer(typeof(T));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, writerSettings))
{
serializer.Serialize(xmlWriter, obj);
}
var serializer = new XmlSerializer(typeof(T));
result = (T)serializer.Deserialize(stream);
[Serializable]
public class Settings
public class TaxRates
{
[XmlElement(ElementName = "TaxRate")]
public decimal ReturnTaxRate;
}
答案 2 :(得分:-1)
检查这个
xsurname.InnerText = p.Forename;
xforename.InnerText = p.Surname;
如果问题依赖于错误的定位行,那么其他可能的共振并不是非常可靠的分类。
关闭前你必须排序。可能这是你的问题。每次保存/关闭后,您都会更改行的顺序。检查一下。