tags:

views:

171

answers:

3

I have an existing XML file that I would like to append without changing the format. Existing File looks like this:

<Clients>
  <User username="farstucker">
    <UserID>1</UserID>
    <DOB />
    <FirstName>Steve</FirstName>
    <LastName>Lawrence</LastName>
    <Location>NYC</Location>
  </User>
</Clients>

How can I add another user using this format? My existing code is:

        string fileLocation = "clients.xml";

        XmlTextWriter writer;

        if (!File.Exists(fileLocation))
        {
            writer = new XmlTextWriter(fileLocation, null);
            writer.WriteStartDocument();

            // Write the Root Element
            writer.WriteStartElement("Clients");

            // End Element and Close
            writer.WriteEndElement();
            writer.Close();
        }
// Append new data here

Ive thought about using XmlDocument Fragment to append the data but Im not certain if I can maintain the existing format ( and empty tags ) without messing up the file.

Any advice you could give is much appreciated.

EDIT Ive changed the code to read the original XML but the file keeps getting overwritten.

+2  A: 

You should use the XmlDocument class to load the whole file, modify it in memory and then write the contents back replacing the original file. Don't worry, it won't mess up your markup, and you can even ask it to preserve non-significant whitespace in the original document using the PreserveWhitespace property (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.preservewhitespace.aspx).

kicsit
I agree with what kicsit says except for the part where he says "XmlDocument", you want to use XDocument and Linq to XML as it is significantly nicer and easier to work with. There's a static load method to read the doc from file and an equivalent save.
Murph
I believe most of the question do not state what version of .NET they are using. LINQ doesn't exist in 2.0 or previous versions.
Nayan
Sorry, Im using 3.5
Farstucker
A: 

Load existing clients.xml into memory( List of Users ) and merge this with new users List and write this data to new clients.xml in your data storage folder after deleting existing clients.xml.

Srinivas Reddy Thatiparthy
A: 

You could try something like this...

        string fileLocation = "clients.xml";

        if (!File.Exists(fileLocation))
        {
            XmlTextWriter writer = new XmlTextWriter( fileLocation, null );
            writer.WriteStartElement( "Clients" );
            writer.WriteEndElement();
            writer.Close();
        }

        // Load existing clients and add new 
        XElement xml = XElement.Load(fileLocation);
            xml.Add(new XElement("User",
            new XAttribute("username", loginName),
            new XElement("DOB", dob),
            new XElement("FirstName", firstName),
            new XElement("LastName", lastName),
            new XElement("Location", location)));
        xml.Save(fileLocation);

That should get you started, just replace the variables with whatever you are using and dont forget to add the System.Xml.Linq namespace.

If youre still having problems post back here and well help you get through it.

Leroy Jenkins