Click here to Skip to main content
14,773,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys

Well the problem is like this i have the following code

XmlTextWriter writer = new XmlTextWriter("D:\\Grop.xml", System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
             //calling the createNode method 
            createNode("123", "3232", "43234","A string of text 1",DateTime.Now.ToString(), writer);
            createNode("123", "3232", "43234", "A string of text 2", DateTime.Now.ToString(), writer);
            createNode("123", "3232", "43234", "A string of text 3", DateTime.Now.ToString(), writer);
            createNode("123", "3232", "43234", "A string of text 4", DateTime.Now.ToString(), writer);

//the create node method
private void createNode(string stud_Id, string post_Id, string group_Id,string desc, string postdate, XmlTextWriter writer)




The problem is this the code above doesnt append to the xml file,it only overwrites the contents of the file everytime i complie the code. How do I get to append each time I compile and run the code?

Thanks in advance
Rob Philpott 3-Apr-13 11:28am
You want to append to the file each time? That would not be valid XML as it has to have just one root node.

The following article discusses XLinq and how to insert/delete. The best option is to load the xml into memory, and update using a technology such as XLinq. This might be of help: XLINQ Introduction Part 3 Of 3[^]

You might be able to insert, but that would require a lot of work since you would have to do it at a very low level. You have to find where the change has to be inserted, then make the space. You would be doing a lot of work. If you need to insert stuff, and don't want to do it in memory, you might want to use a database.
I think you'll have to read the XML file in first (e.g XmlTextReader ?), insert the nodes where you want them (i.e. in memory) and then write the whole thing back to the file.

sijimann 3-Apr-13 11:35am
Hi Sorry to be much of a a bother wouldn't that be reduce performance?, because i am creating a social site that is suppose to accommodate the whole world
Ian A Davidson 3-Apr-13 12:49pm
Reduce performance from what? If you want to append to XML you will have to read a certain amount of it in anyway, in order to ensure you're putting stuff at the right point so that it does not become invalid XML, as Rob has pointed out. If performance is an issue, you're probably storing the information in the wrong format to start with - how about using a database? However, if you want to stick to using an XML file, the solution Pallavi has found looks like a good one. Or, if you are always writing nodes in the same place at the end of the file you could possibly write your own code to "seek" to the last tag from where you want to rewrite the ending. However, as far as performance goes, appending a line (e.g. text-delimited) to a plain text file would be quicker than either of those methods, since you can simply open for appending and move to the end of the file before writing it. It depends what you are using the data for, and how easy it would be to parse it back in. Regards, Ian.
lewax00 3-Apr-13 20:02pm
Have to agree with this, XML is the wrong format to be using when performance is a concern. And files are a poor choice in general if it needs to be able to be edited by multiple threads/processes simultaneously (as it probably would be in the case of a website).
"Insert the nodes" is correct, but it should be said that a valid APPEND to an XML file would result in a not well-formed XML. Appending is illegal.
Please see my answer.
try this article it will help u..check 1 Answer[^]
Ian A Davidson 3-Apr-13 12:50pm
+5. Well found.
sijimann 4-Apr-13 2:55am
Thanks a lot that worked and did the trick
Appending anything to XML file (except perhaps a comment of blank space) would always make the resulting file not well-formed as XML, this is because the XMK structure is strictly hierarchical, with as simple root note (not counting XML prolog).


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900