将多个记录添加到XML文件

时间:2013-09-04 04:17:10

标签: c# wpf xml

我想在xml文件中添加多条记录,这是我正在使用的代码,

XmlTextWriter xwriter = new XmlTextWriter("C:\\Users\\Desktop\\TestFolder\\Xdoc1.xml", Encoding.UTF8);
xwriter.Formatting = Formatting.Indented;
xwriter.WriteStartElement("Employee");
xwriter.WriteStartElement("Person");
xwriter.WriteStartElement("Name");
xwriter.WriteString(textBox1.Text);
xwriter.WriteEndElement();
xwriter.WriteStartElement("Designation");
xwriter.WriteString(textBox2.Text);
xwriter.WriteEndElement();
xwriter.WriteStartElement("Employee ID");
xwriter.WriteString(textBox3.Text);
xwriter.WriteEndElement();
xwriter.WriteStartElement("Email");
xwriter.WriteString(textBox4.Text);
xwriter.WriteEndElement();
xwriter.WriteEndElement();
xwriter.WriteEndElement();
xwriter.Close();

此代码的问题是只能添加一条记录。当我尝试添加第二条记录时,之前的记录会被覆盖。

2 个答案:

答案 0 :(得分:0)

这里不需要转换xmlWriter类。

string xmlFile = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Candidates.xml");

xmldoc = new XmlDocument();
xmldoc.Load(xmlFile);
root = xmldoc.DocumentElement;

try
{
    XmlNode CandidateNode = xmldoc.CreateNode(XmlNodeType.Element, "Candidate", "");
    XmlNode id = xmldoc.CreateNode(XmlNodeType.Element, "CandidateId", "");
    id.InnerText = "1";
    CandidateNode.AppendChild(id);
    XmlNode subPositionId = xmldoc.CreateNode(XmlNodeType.Element, "SubPositionId", "");
    subPositionId.InnerText = candidate.PositionId.ToString();
    CandidateNode.AppendChild(subPositionId);
    XmlNode firstName = xmldoc.CreateNode(XmlNodeType.Element, "FirstName", "");
    firstName.InnerText = candidate.FirstName;
    XmlNode lastName = xmldoc.CreateNode(XmlNodeType.Element, "LastName", "");
    lastName.InnerText = candidate.LastName;
    CandidateNode.AppendChild(firstName);
    CandidateNode.AppendChild(lastName);
    root.AppendChild(CandidateNode);
    xmldoc.Save(xmlFile);

这会对你有帮助。

答案 1 :(得分:0)

Linq to XML使xml任务更容易。请看下面的代码。

if (!System.IO.File.Exists("D:\\Employees.xml"))
{
    XElement element = new XElement("Employees");
    element.Save("D:\\Employees.xml");
}

XElement doc = XElement.Load("D:\\Employees.xml");

XElement employee = new XElement("Employees",
                                    new XElement("Employee",
                                                    new XElement("Person",
                                                                new XElement("Name",
                                                                            textBox1.Text),
                                                                new XElement("Designation",
                                                                            textBox2.Text),
                                                                new XElement("EmployeeID",
                                                                            textBox3.Text),
                                                                new XElement("Email",
                                                                            textBox4.Text))));

doc.Add(employee);
doc.Save("D:\\Employees.xml");