If I use this code to create a new xml file:
new XDocument(
new XElement("user",
new XElement("name", textBox1.Text),
new XElement("age", textBox2.Text),
new XElement("rollNo", textBox3.Text)
)
).Save("gsv.xml");
Then how should I be adding the new data below the existing file? How to append this file?

14 answers
I'd do all that something like this:
answered one year ago by:
17279
If you wanted to append a new element, email say, to the root element (user) and then resave the XML file, you could do it like this:
var doc = XDocument.Load("gsv.xml");var xe = new XElement("email", textBox4.Text);
doc.Root.Add(xe);
doc.Save("gsv.xml");
EDIT - See comments below
If you want to add new users then, when you create the file, you need to have a different root element - users, say - to which you can then append as many user child elements as you like:
SECOND EDIT - explaining 'var'
'var' is a new keyword introduced in C# 3.0. You don't need to import any specific namespace in order to use it.
It tells the compiler to infer the type of a local variable from what you're assigning to it. So the line:
var doc = XDocument.Load("gsv.xml");means that 'doc' is inferred to be of type XDocument because that's what XDocument.Load returns. It's therefore precisely equivalent to this line but avoids the repetition:
XDocument doc = XDocument.Load("gsv.xml");In this situation its use is optional; if you don't like it - and many folks don't - stick with the second form.
However, it has to be used when you're declaring an 'anonymous type'. For example:
var v = new{Name = "Fred", Age = 30};Console.WriteLine("{0} is aged {1}", v.Name, v.Age);
Apart from the declaration of anonymous types, I only use 'var' myself to avoid repetition i.e. where what's on the RHS of the '=' sign includes the type of the variable. I don't like to see it used in this scenario:
answered 2 years ago by:
17279
412
Wow thx I had still not reached this editing of the same field. My xml was having data (name - textBox1, department - textBox2, Extn - textBox2). I was asking that after entering 1 name into the xml.... if I enter another name then it should be added in the end of the existing xml file. . . . I'm talking about entering all these textBox1,2,3 and adding the data in the end of existing xml file.
17279
Please see my edit above.
412
var doc ......... this var is what? do i need to include any namespace for this?
17279
Please see my second edit above.
412
Somehow this was not working in the office. Some problem with VStudio I think. It was not able to understand "var"..... I'm at home and waiting for code to get downloaded in mail, due to some internet prob the mails are not getting downloaded. But still I will accept the answer as best answer bcoz of the explanation u wrote. Even a person like me was able to understand means that U have explained it wonderfully. Thx for always taking pains to reply with a wonderful solution. :)
17279
The only reason I can think of why 'var' wouldn't work is if you're using VS 2005 (C# 2.0) in the office rather than VS 2008. However, in that case, LINQ to XML (XDocument, XElement etc) wouldn't work either so I'm a bit puzzled but, never mind, you can always write the types in full.
412
Right I'm having VS 2005 Express edition with Framework 3.0 in office computer. I downloaded and installed a patch from Microsoft.com which added xml.linq into the list of COMs. I had been asking the admin to reinstall the application & this patch. But silly fellows are not listening. Even the dynamic help (suggestions while typing) have been affected. Prob was that other computers in office are still working in stone age so I can't go-on to add higher frameworks/versions to develop something which won't work on other computers in the network.
Not able to add comment, so I'm writing an answer.....
I'm planning to create something like department's news buletin where I can post some news like vendor presentations, departmental meeting, or other departmental communications.
Someday there might not be any news, but moreover I wanna display the news date-wise in a table. Something like this
Date: 09/Mar/10
News5 Some news.
News4 Some news.
News3 Some news.
News2 Some news.
News1 Some news.
Date: 07/Mar/10
News3 Some news.
News2 Some news.
News1 Some news.
, and I'm not able to create an xml which will easily help to create such an xsl to format. Biggest prob is to sort the xml depending on date tag.
data.xml I've created was:-
<news>
<date>
<on>08-mar-2010</on>
<time>10:30</time>
<title>News 1</title>
<item>News item 1 goes in here.</item>
</date>
<date>
<on>08-mar-2010</on>
<time>10:32</time>
<title>News 2</title>
<item>News item 2 goes in here.</item>
</date>
<date>
<on>08-mar-2010</on>
<time>10:32</time>
<title>News 1</title>
<item>News item 1 goes in here.</item>
</date>
<date>05-mar-2010
<on>05-mar-2010</on>
<time>13:32</time>
<title>News 1</title>
<item>News item 1 goes in here.</item>
</date>
</news>
answered one year ago by:
412
17279
Do you mean that the news items won't necessarily be added to the XML file in chronological order? If so, then as long as there's a date attribute for each item, you could use XSL to sort them by including an 'order-by' attribute. See http://msdn.microsoft.com/en-us/library/ms950734.aspx for an example.
If the XML file isn't so large that loading it into memory is a problem, then an easy way to do this would be to use XML serialization to load the news items into a list which you can then sort in descending order by their date and time.
Something like the following:
answered one year ago by:
17279
412
these news items are not a gr8 thing, just a departmental utility to share news (like time/venue of department meeting, client visit, ISO audits, etc) with the remaining people in the department. I thought I'll just distribute this exe file so that ppl can see if there's any Important announcement / news for today.
Oh my goddddddd!!!!!!!!!!!!!!!!
If I've to know & write such a scary logic/code for fetching just two fields from an xml file, then I think u should guide me to a better way of writing the xml file. I think its more important to know the easily retrievable structure of xml file.
Okie let's say if I write my xml something like this:
Is there a esier way to workout with xmls selections just like we do in xslt???
-----Previous Question-----
I'm just trying to populate the dorpdownlist to read all employee names and the selectedchangeindex should fill the department (in textBox1) of the "selected" employee (name)
No success :(
answered one year ago by:
412
There are numerous ways to read an XML file but, if the file were as simple as that, then using LINQ to XML would probably be the easiest:
answered one year ago by:
17279
I'm trying to add the feature of "Editing" the exisitng xml (in same folder) using XmlSerializer as per ur code
I'm reading the description values into the winform:
What I'm wishing for is that: On Submit button, I need to check if the comboBox1.SelectedIndex exists in xml.
if (comboBox1.Text== existing tag/value in xml
// then append description, end date, last date of that .SelectedIndex tag
else
// add it in the xml file
answered one year ago by:
412
Try:
EDIT
I'm sure there was a question that the above code was answering but it seems to have disappeared now!
Anyway, to deal with your last question, the code to add a new entry to the XML file would be something like this (notice that File.Create automatically overwrites an existing file):
answered one year ago by:
17279
EDIT: -
How can I insert the following tag into my new xml file which is created using C# code????
ERROR
Error in xml when "Edit" function is called:
The Tag <issues> is coming wrongly as: <issues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> in the xml generated using "Edit" code...
and I don't want to replace the .xml file. I just want that particular entry of that SelectedIndex field should be edited & replaced in the existing file.
On selectedIndexChanged I'm fetching the values of that Index into the Form:
If the user is making any change in these existing values & presses "Edit" button then the same Tag on the selected Index should be rewritten in the xml.
Or should I delete that complete selected tag and rewrite the new information into the xml???
Any help?????
answered one year ago by:
412
As far as I can see, there's no way to write a stylesheet processing instruction directly to a file using the XmlSerializer. What you can do is to serialize to a memorystream initially, load the stream into an XmlDocument, insert the processing instruction into that and then save it to the file.
Also, when using the XmlSerializer, you have no choice but to replace the entire file or to serialize to a new file - you can't insert or alter individual nodes.
However, it is possible to prevent the standard namespace references being added to the root node and I've included revised code to do that and add the stylesheet processing instruction below:
EDIT
What I don't understand here is why the code gets rid of the namespaces on my machine but not on yours - it just changes them to something else!
Anyway, let's see if we can get rid of them by editing them out of the XmlDocument before saving to the file. Try adding the following line immediately before doc.Save(filePath):
If this works, then it should get rid of the null reference exception as well because the deserialization should then be successful.
SECOND EDIT (cleaner version of above code)
answered one year ago by:
17279
Thhis code had perfectly added the stylesheet code into the new xml, but the code
Just like in previous xml:
Is giving an error. I'm not able to understand the need of insertion of this xmlns thing in <issues> tag... :(
answered one year ago by:
412
17279
You shouldn't now be getting any XML namespace references at all in the 'issues' node. Adding the empty namespace should remove them all and did do when I tested it.
SECOND EDIT
The same error thing is appearing in the Edit code also
But here we've not defined any DocumentElement, then how to capture/change it's Attributes in edit????
Okie let's replace the code and Add the same MemoryStream function and rewrite the XmlProcessingInstructions in the xml, but this logic behind "Edit" code is simply editing the xml file. Whereas the "Edit" button should be editing a particular TAG of the xml.
Let me explain myself.... the Page_Load code had fetched all the Titles of Issues into the DropDownList. Then the SelectedIndexChanged code fetches all related information of that Title into remaining fields of the form
Now... now.... now.....
If the user makes any change in the Description or Start Date or End Date fields in the form..... Then
The respective Tag of the xml should be rewritten with this new Information.
This is what I'm meaning by "Edit" here. Whereas the code right now is adding another field in the xml.
Shall I delete the previous Tag and rewrite this as a new tag in xml? What do u say?
answered one year ago by:
412
17279
Please see my edit.
17279
All I've done is to serialize 'issues' to a memory stream rather than directly to disk. I've then loaded the stream into an XmlDocument so we can edit it before finally writing it to disk. So, whenever, you're using the serializer you should be able to take the same steps to insert the stylesheet processing instruction and get rid of the standard namespaces that are otherwise added. Also, you might as well get rid of the 'XmlSerializerNamespaces' code which isn't achieving anything useful on your machine. See my second edit for a 'cleaner' version of the code.
17279
The rationale of using XML (de)serialization in this way, is that you have an object in memory (namely 'issues') which represents the XML file on disk. If you add a new issue, then you should add that to issues.InstList. If you change an existing issue, then you should change the corresponding 'issue' object in issues.InstList. Then, when you serialize 'issues' back to the disk file, it overwrites the existing file and corresponds exactly with what's in memory. So, the last lot of code I posted is solely concerned with how to serialize the object to disk in the way you want (no namespaces etc). Before you can call that code, you need to ensure that your 'issues' object has been updated with the latest addtions, deletions, or amendments. It's as simple as that:)
Deleting a particular Tag from xml
My xml file is something like this:
How should I delete complete <Parent> tag if (comboBox1.Text = "Something 1") ????
answered one year ago by:
412
This is off the top of my head and so may not be quite right:
answered one year ago by:
17279