Jitesh Byahut 6/23/2014 2801

How To Update Xml In C#

Use System.Xml.Linq Namespace.

Here few examples are shown to how to update xml elements and attributs in different types of xml documents.

Example 1

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student>

    <Name>Jitesh</Name>

    <Email>jitesh@asphelps.com</Email>

    <City>Buxar</City>

  </Student>

</Students>

        XDocument document = XDocument.Load(@"D:\Student.xml");

 

        XElement element = document.Descendants("Student")

                                   .Where(i => i.Element("Name").Value == "Jitesh").Single();

        element.SetElementValue("Name", "Jitesh kumar");

        element.SetElementValue("Email", "Jiteshkumar@asphelps.com");

        element.SetElementValue("City", "Buxar, Dumraon");

        document.Save(@"D:\Student.xml");

Output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student>

    <Name>Jitesh kumar</Name>

    <Email>Jiteshkumar@asphelps.com</Email>

    <City>Buxar, Dumraon</City>

  </Student>

</Students>

Example 2

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student Id="1">

    <Name>Jitesh</Name>

    <Email>jitesh@asphelps.com</Email>

    <City>Buxar</City>

  </Student>

</Students>

        XDocument document = XDocument.Load(@"D:\Student1.xml");

 

        XElement element = document.Descendants("Student")

                                                                                   .Where(i => i.Attribute("Id").Value == "1").Single();

        element.SetElementValue("Name", "Jitesh Byahut");

        element.SetElementValue("Email", "jiteshbyahut@asphelps.com");

        element.SetElementValue("City", "Dumraon, Buxar");

        document.Save(@"D:\Student1.xml");

Output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student Id="1">

    <Name>Jitesh Byahut</Name>

    <Email>jiteshbyahut@asphelps.com</Email>

    <City>Dumraon, Buxar</City>

  </Student>

</Students>

Example 3

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student Id="1" Name="Jitesh" Email="jitesh@asphelps.com" City="Buxar" />

</Students>

        XDocument document = XDocument.Load(@"D:\Student2.xml");

 

        XElement element = document.Descendants("Student")

                                                                                 .Where(i => i.Attribute("Id").Value == "1").Single();

        element.SetAttributeValue("Name", "Jitesh Byahut");

        element.SetAttributeValue("Email", "jiteshbyahut@asphelps.com");

        element.SetAttributeValue("City", "Dumraon, Buxar");

        document.Save(@"D:\Student2.xml");

Output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student Id="1" Name="Jitesh Byahut" Email="jiteshbyahut@asphelps.com" City="Dumraon, Buxar" />

</Students>

Example 4

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student>

    <Name>Jitesh</Name>

    <Email>jitesh@asphelps.com</Email>

    <City>Buxar</City>

  </Student>

  <Education>

    <Degree>B.Tech</Degree>

    <Branch>ECE</Branch>

    <University>RTU</University>

  </Education>

  <Contact>

    <Mobile>8431214496</Mobile>

    <Email>jitesh123.2008@gmail.com</Email>

  </Contact>

</Students>

        XDocument document2 = XDocument.Load(@"D:\Student3.xml");

 

        XElement student = document2.Descendants("Student").Single();

        student.SetElementValue("Name", "Jitesh Byahut");

        student.SetElementValue("Email", "jiteshbyahut@asphelps.com");

        student.SetElementValue("City", "Dumraon, Buxar");

 

        XElement education = document2.Descendants("Education").Single();

        education.SetElementValue("Degree", "Graduation");

        education.SetElementValue("Branch", "Electronics & Communication");

        education.SetElementValue("University", "Rajasthan Technical University");

 

        XElement contact = document2.Descendants("Contact").Single();

        contact.SetElementValue("Mobile", "8560839802");

        contact.SetElementValue("Email", "jiteshbyahut@asphelps.com");       

 

        document2.Save(@"D:\Student3.xml");

Output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student>

    <Name>Jitesh Byahut</Name>

    <Email>jiteshbyahut@asphelps.com</Email>

    <City>Dumraon, Buxar</City>

  </Student>

  <Education>

    <Degree>Graduation</Degree>

    <Branch>Electronics &amp; Communication</Branch>

    <University>Rajasthan Technical University</University>

  </Education>

  <Contact>

    <Mobile>8560839802</Mobile>

    <Email>jiteshbyahut@asphelps.com</Email>

  </Contact>

</Students>

Example 5

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student>

    <Name>Jitesh</Name>

    <Email>jitesh@asphelps.com</Email>

    <City>Buxar</City>

    <Education>

      <Degree>B.Tech</Degree>

      <Branch>ECE</Branch>

      <University>RTU</University>

    </Education>

  </Student>

</Students>

        XDocument document2 = XDocument.Load(@"D:\Student4.xml");

 

        XElement student = document2.Descendants("Student").Single();

        student.SetElementValue("Name", "Jitesh Byahut");

        student.SetElementValue("Email", "jiteshbyahut@asphelps.com");

        student.SetElementValue("City", "Dumraon, Buxar");

 

        XElement education = student.Descendants("Education").Single();

        education.SetElementValue("Degree", "Graduation");

        education.SetElementValue("Branch", "Electronics & Communication");

        education.SetElementValue("University", "Rajasthan Technical University");

 

        document2.Save(@"D:\Student4.xml");

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<Students>

  <Student>

    <Name>Jitesh Byahut</Name>

    <Email>jiteshbyahut@asphelps.com</Email>

    <City>Dumraon, Buxar</City>

    <Education>

      <Degree>Graduation</Degree>

      <Branch>Electronics &amp; Communication</Branch>

      <University>Rajasthan Technical University</University>

    </Education>

  </Student>

</Students>


Please give your feedback for improving this page