Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how could i insert, read, update and delete data from xml file with using C#?
Posted

There are various ways to achieve the same.

If you want to keep reading sequentially, you can use XmlReader.

If you want to read/update/delete/insert at particular location, you can use two approaches
1. XmlDocument and XPath.
2. LinQ to XML

For XmlDocument approach, First you declare variable and load xml.

C#
XmlDocument xDoc = new XmlDocument();
xDoc.Load("YourFile.Xml");

Then, you read like
C#
string nodeValue = xDoc .DocumentElement.SelectSingleNode("NodeName").InnerText;


You can update value like
C#
xDoc.DocumentElement.SelectSingleNode("NodeName").InnerText = newValue;

Note, that in above code, "NodeName is actually Xpath. You can give it like Node/ChildNode/GrandChildNode and so on. You can find more details about xPath at http://www.w3schools.com/xpath/default.asp[^]

Hope that helps. If it does mark it as answer/upvote.

Thanks
Milind
 
Share this answer
 
Comments
Member 10408516 27-Mar-14 6:56am    
When there are the nodes of the same name and i want to add inner text to the last node what should i do
eg
<book>
<Pg1 type="textbook">
<content pgno="12">11 22
<content pgno="120">112 asc aaa 1sw
<content pgno="200">112 asc 123 123
</Pg1>


Herei want to add some data "aaa knj rta" along with "112 asc aaa 1sw" of the second content tag what is the code i need to write
Better and easy way to do is make use of LINQ TO XML to insert,update and delete

Check here all examples are given for LINQ TO XML - http://msdn.microsoft.com/en-us/vstudio/bb688087.aspx[^]
 
Share this answer
 

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