Jitesh Byahut 6/23/2014 2022

How to insert into xml in C#

Use System.Xml.Linq Namespace.

Here few examples are shown to how to insert xml elements and attributes 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");

 

        document.Element("Students").Add(

            new XElement("Student",

                    new XElement("Name", "Amit"),

                    new XElement("Email", "Amit@asphelps.com"),

                    new XElement("City", "Dumraon")

                    )

            );

 

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

Output:

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

<Students>

  <Student>

    <Name>Jitesh</Name>

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

    <City>Buxar</City>

  </Student>

  <Student>

    <Name>Amit</Name>

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

    <City>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 document1 = XDocument.Load(@"D:\Student1.xml");

 

        document1.Element("Students").Add(

            new XElement("Student",

                    new XAttribute("Id", "2"),

                    new XElement("Name", "Amit"),

                    new XElement("Email", "Amit@asphelps.com"),

                    new XElement("City", "Dumraon")

                    )

            );

 

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

Output:

<?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>

  <Student Id="2">

    <Name>Amit</Name>

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

    <City>Dumraon</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 document2 = XDocument.Load(@"D:\Student2.xml");

 

        document2.Element("Students").Add(

 

                new XElement("Student",

                    new XAttribute("Id", "2"),

                    new XAttribute("Name", "Amit"),

                    new XAttribute("Email", "amit@asphelps.com"),

                    new XAttribute("City", "Dumraon")

                    )

 

            );

 

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

Output:

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

<Students>

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

  <Student Id="2" Name="Amit" Email="amit@asphelps.com" City="Dumraon" />

</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>

</Students>

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

 

        document3.Element("Students").Add(

                new XElement("Contact",

                    new XElement("Mobile", "8431214496"),

                    new XElement("Email", "jitesh123.2008@gmail.com")

                    )

            );

 

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

Output:

<?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>

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 document4 = XDocument.Load(@"D:\Student4.xml");

 

        document4.Element("Students").Add(

                 new XElement("Student",

                     new XElement("Name", "Amit"),

                     new XElement("Email", "amit@asphelps.com"),

                     new XElement("City", "Dumraon"),

 

                     new XElement("Education",

                     new XElement("Degree", "B.Tech"),

                     new XElement("Branch", "ECE"),

                     new XElement("University", "RTU")

                     )

                )

 

         );

 

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

Output:

<?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>

  <Student>

    <Name>Amit</Name>

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

    <City>Dumraon</City>

    <Education>

      <Degree>B.Tech</Degree>

      <Branch>ECE</Branch>

      <University>RTU</University>

    </Education>

  </Student>

</Students>



Please give your feedback for improving this page