0

我想在 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();

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

4

2 回答 2

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);

这将对您有所帮助。

于 2013-09-04T04:47:46.733 回答
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");
于 2013-09-04T06:06:16.987 回答