Showing posts with label How to delete xml in C#. Show all posts
Showing posts with label How to delete xml in C#. Show all posts

Tuesday, July 22, 2014

How to delete xml in C#

Example 1
<?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>
        XDocument document = XDocument.Load(@"D:\Student.xml");

        document.Element("Students").Elements("Student").Remove();

        document.Save(@"D:\Student.xml");
Example 2
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student Id="1">
    <Name>Amit</Name>
    <Email>Amit@asphelps.com</Email>
    <City>Dumraon</City>
  </Student>
</Students>
XDocument document1 = XDocument.Load(@"D:\Student1.xml");

document.Element("Students").Elements("Student").Where(i=>i.Attribute("Id").Value == "1").Remove();

document.Save(@"D:\Student1.xml");
Example 3
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
  <Student Id="1" Name="Jitesh Byahut" Email="jiteshbyahut@asphelps.com" City="Dumraon, Buxar" />
  <Student Id="2" Name="Amit" Email="amit@asphelps.com" City="Dumraon" />
</Students>
  XDocument document = XDocument.Load(@"D:\Student3.xml");

  document.Element("Students").Elements("Student").Where(i=>i.Attribute("Id").Value=="1").Remove();

  document.Save(@"D:\Student3.xml");
Example 4
<?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>
  <Contact>
    <Mobile>8431214496</Mobile>
    <Email>jitesh123.2008@gmail.com</Email>
  </Contact>
</Students>
        XDocument document = XDocument.Load(@"D:\Student3.xml");
        // If you want to delete Student section
        document.Element("Students").Elements("Student").Remove();
        // If you want to delete Education section
        document.Element("Students").Elements("Education").Remove();
        // If you want to delete Contact section
        document.Element("Students").Elements("Contact").Remove();

        document.Save(@"D:\Student3.xml");
Example 5
<?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>
        XDocument document = XDocument.Load(@"D:\Student4.xml");
        // If you want to delete Student section
        document.Element("Students").Elements("Student").Remove();
        // If you want to delete Education section
        document.Element("Students").Elements("Student").Elements("Education").Remove();


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