How to update xml file without using Linq?

update xml file in c# using xdocument
read and edit xml file in c#
query xml document with linq c#
read and update xml file in c#
linq to xml create xml document
edit xml file c#
linq to xml youtube
add new node to existing xml file c#

I have this xml file:

<students>
<student>
    <name>Mark</name>
    <age>20</age>
</student>
<student>
    <name>Adam</name>
    <age>32</age>
</student>

I want to change the age of Adam, for that I did:

XmlDocument doc = new XmlDocument();
doc.Load("/path/to/sutdents.xml");
XmlNode n = doc.SelectSingleNode("/students/student[name='" + student.name + 
"']");
if (n != null)
{
   n.SelectNodes("age").Item(0).FirstChild.Value = new_value;
}

but I get "n" as a null value. I tried another way to do that by searching for the node using foreach and make change, but I get Null Exception:

XmlDocument doc = new XmlDocument();
doc.Load("/path/to/sutdents.xml");
XmlNodeList nodes = doc.SelectNodes("/students/student");
foreach (XmlNode node in nodes)
 {
   if (node.FirstChild.InnerText == student.name)
    {
     node.SelectSingleNode("age").InnerText = new_value;
    }
  }

what I missing here please? Thanks in advance


Your Code is Correct here is a screen shot of your debugged code with same xml and mentioned code, check you are passing student.name with correct value and case sensitive.

How to update XML attributes using Linq to XML in C#, You can accomplish the update with your method by adding one more inner loop: If you want to use Linq, you should switch to XDocument: In case your xml is not valid document (without <?xml.. tag) then you can return  Update Data to XML Using LINQ to XML. Today, in this article let’s play around with one of the interesting and most useful concepts in LINQ to XML. Join Public Speaking Virtual Conference


I tried to run your code. that is work! check what is the value of student.name.

XML [sutdents.xml]

<?xml version="1.0" standalone="yes"?>
<students>
  <student>
    <name>Mark</name>
    <age>20</age>
  </student>
  <student>
    <name>Adam</name>
    <age>32</age>
  </student>
</students>

sample code:

XmlDocument doc = new XmlDocument();
doc.Load("sutdents.xml");
XmlNode n = doc.SelectSingleNode("/students/student[name='Adam']");
if (n != null)
{
    n.SelectNodes("age").Item(0).FirstChild.Value = "44";
}
doc.Save("sutdents.xml");

Part 4 Modifying xml document using linq to xml, Text version of the video http://csharp-video-tutorials.blogspot.com/2014/08/part-​4-modifying Duration: 12:50 Posted: Aug 27, 2014 The XML file (see below) has many node’s. However, I wish to update the Price for each selected book. I’ll allow my users to change the price on my web page. The user will select a book from the <select> dropdown element and click the Show Details button. Once clicked, the click event at the code behind section will fetch misc details about the book from the XML file (matching the selected book), and show it.


i hope this helps;

When Using foreach:

 XmlNodeList studentsList = doc.ChildNodes[0].SelectNodes("student");

foreach (XmlNode node in studentsList)
{
  if (node.ChildNodes[0].InnerText == student.name) //name
   {
      node.ChildNodes[1].InnerText = new_value; //age
   }
}

Update xml file without Linq, Hello I hae this xml file: <students> <student> <name>Mark</name> <age>20</​age> </student> <student> <name>Adam</name> <age>32</ To add the xml element as the first element use AddFirst() method. To add the xml element in a specific location in the XML Document, use AddBeforeSelf() or AddAfterSelf().


Try this using XDocument:

private static void UpdateXML(string xmlpath, string studentName, int age)
{
    var doc = XDocument.Load(xmlpath);

    //Get student
    var student = doc.Descendants("student").Where(att => att.Element("name") != null && att.Element("name").Value.Equals(studentName)).FirstOrDefault();
    if (student != null)
        student.Element("age").Value = age.ToString();
    doc.Save(xmlpath);
}

Update Data to XML Using LINQ to XML, No unread notification. loading View All Notifications Question: What is update data to XML using LINQ to XML? In simple terms "It provides flexibility to update data in XML with the help of a LINQ query." Step 1: Create a Load(​Server.MapPath("Employee.xml"));; var updateQuery = from r in document. Next Steps. You have seen how easy it is to read XML documents with LINQ. You can use LINQ to read XML documents from the file system, from a remote web service, or from an in-memory XML content. We will look at using LINQ to XML to manipulate XML data in future tips.


XML Manipulation In C#, XML stands for Extensible Markup Language file format, which is operating systems, new applications, or new browsers, without losing data. The following is the LINQ to XML(XDocument) class hierarchy, which will help to understand it more. Sometimes, we need to change/update XML node value. In addition to these LINQ capabilities, LINQ to XML provides an improved XML programming interface. Using LINQ to XML, you can: Load XML from files or streams. Serialize XML to files or streams. Create XML from scratch by using functional construction. Query XML using XPath-like axes.


LINQ to XML Instead of a Database, However, XML files are very handy for storing data without the overhead of a database. to using LINQ to read, insert, update and delete data from an XML file. Add a new Elements to the xml file. The Linq expression to add a new element to the xml file is similary to reading from that file. However, you will use the Add method to add a new element to the file. In the example code below, a new element that has "AJAX" title attribute, and two sub-elements: author and year is added to the file.


C# tutorial, Simetimes, you might want change or udate a element of xml file. It is not hard to do this task by using Linq. The process is simple. You need to query the elements​  This article describes how to add, delete, update, and select nodes in XML in C#. We will use mostly XDocument and XMLDocument class to manipulate XML data. XML stands for Extensible Markup Language file format, which is used to create common information formats and share both the format and the data on the World Wide Web, intranet, etc.