I have an existing XML file that I would like to add without changing the format. The existing file is as follows:
<Clients>
<User username="farstucker">
<UserID>1</UserID>
<DOB />
<FirstName>Steve</FirstName>
<LastName>Lawrence</LastName>
<Location>NYC</Location>
</User>
</Clients>
How to add another user to this format? My existing code is:
string fileLocation = "clients.xml";
XmlTextWriter writer;
if (!File.Exists(fileLocation))
{
writer = new XmlTextWriter(fileLocation, null);
writer.WriteStartDocument();
writer.WriteStartElement("Clients");
writer.WriteEndElement();
writer.Close();
}
I thought about using an XmlDocument Fragment to add data, but I'm not sure if I can support the existing format (and empty tags) without messing up the file.
Any advice you could give is greatly appreciated.
EDIT Ive changed the code to read the source XML, but the file continues to be overwritten.
source
share