I have done this code so far works well, but the only issue is that the XML file gets deleted every time I use the method addUser, I want the code to continue writing under it
the code I wrote in c#:
public static void AddUser(PersonData pd)
{
XmlTextWriter xWriter = new XmlTextWriter("D:\\PersonData.Xml", Encoding.UTF8);
xWriter.Formatting = Formatting.Indented;
xWriter.WriteStartElement("User");
xWriter.WriteAttributeString("idNumber", pd.IdNumber1);
xWriter.WriteStartElement("Firstname");
xWriter.WriteString(pd.FirstName1);
xWriter.WriteEndElement();//<FirstName>
xWriter.WriteStartElement("LastName");
xWriter.WriteString(pd.LastName1);
xWriter.WriteEndElement();//<LastName>
xWriter.WriteStartElement("DateOfBirth");
xWriter.WriteString(pd.DateOfBirth1.ToString());
xWriter.WriteEndElement();//<DateOfBirth>
xWriter.WriteStartElement("Address");
xWriter.WriteString(pd.Address1);
xWriter.WriteEndElement();//<Address>
xWriter.WriteEndElement();//<user>
xWriter.Close();
}
the XML output:
<User idNumber="316447077">
<Firstname>majd</Firstname>
<LastName>sadi</LastName>
<DateOfBirth>29/03/1998 14:54:50</DateOfBirth>
<Address>hohos</Address>
</User>
the XML output that I want to do:
<User idNumber="316447077">
<Firstname>majd</Firstname>
<LastName>sadi</LastName>
<DateOfBirth>29/03/1998 14:54:50</DateOfBirth>
<Address>hohos</Address>
</User>
<User idNumber="316447077">
<Firstname>majd</Firstname>
<LastName>sadi</LastName>
<DateOfBirth>29/03/1998 14:54:50</DateOfBirth>
<Address>hohos</Address>
</User>
Assuming you have a root node to your XML (not valid XML without it), you could do this with XElement: