0

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

1
  • Do you want to modify the XML by keeping the Schema or don't you care about Schema modifications? Commented Sep 10, 2009 at 4:52

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

0

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)

Comments

-1

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#

1 Comment

Not closing the FileStream, not having a using/calling dispose on the FileStream object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.