tags:

views:

723

answers:

5

How i insert data into xml file using Windows Application in .net?

A: 

There's a very good bit of documentation about working with the DOM in .NET here.

Do you have a specific example of what you're trying to do? That way you'll get a clearer answer/example.

Nader Shirazie
A: 

If your xml file is not huge, one of the easiest option is to use XmlDocument. Just load your xml and append your new xml nodes where you want in the xml file.

Here the documentation about XmlDocument: MSDN.

Code Example:

XmlDocument dom = new XmlDocument();
dom.Load("filename");

//Append a new node
XmlElement newNode = dom.CreateElement("NewNode");
dom.DocumentElement.AppendChild(newNode);

Each XmlNode (XmlElement, XmlAttribute, XmlText, etc..) has different methods to insert before, insert after, append, remove a xml node. So, you can do pretty much anything with your DOM.

In the case, your xml file is big, XmlDocument can really hurt the performance of your application. I would recommend to use a combination of XmlReader and XmlWriter or XDocument.

Francis B.
+1  A: 

This is a very general question. There are several common approaches, depending on your target use case.

bobbymcr
A: 

Here is one for C#

//The path to our config file   
string path = "Config.xml";
//create the reader filestream (fs)  
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
//Create the xml document
System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();
//Load the xml document  
CXML.Load(fs);     
//Close the fs filestream  
fs.Close();       
// create the new element (node)  
XmlElement newitem = CXML.CreateElement("Item");
// Put the value (inner Text) into the node   
newitem.InnerText = "This is item #" + (CXML.DocumentElement.ChildNodes.Count + 1).ToString() + "!";               
//Insert the new XML Element into the main xml document (CXML)       
CXML.DocumentElement.InsertAfter(newitem, CXML.DocumentElement.LastChild);                
//Save the XML file           
 FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);       
CXML.Save(WRITER);   
//Close the writer filestream    
WRITER.Close();

You a can find a Good article - Working with XML files in C#

Sauron
Not closing the FileStream, not having a using/calling dispose on the FileStream object.
Yuriy Faktorovich
So I want to add the full code here.?
Sauron
A: 

If you know the schema (XSD) of your XML you can use xsd.exe to generate classes to parse these XML files. If you don't know the schema, xsd.exe can TRY to extrapolate it for you.

Then it is easy to add properties to the generated classes (modifies the original Schema!) or use the existing properties to insert/change what you want. This is a fast way to perform the task.

If the Schema is not too complicated I would do the reading/writing by hand using XmlSerialization attributes as the code will definitively be cleaner. As long as the XML is not using features like Mixed mode it will work (there are some limitations in the XML serializing framework, usually not critical if you stick to good practices)

jdehaan