Jitesh Byahut 6/23/2014 3836

Create Xml File Using Linq to Xml

LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the .NET Framework programming languages. LINQ to XML enables you to easily convert a sequence into an XML document. Here few examples are shown to how to create different types of xml documents.

Use System.Xml.Linq Namespace.

Example 1

In this example, we will learn how to create below sample xml file using Linq to Xml.

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

<Students>

  <Student>

    <Name>Jitesh</Name>

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

    <City>Buxar</City>

  </Student>

</Students>

Use below code for creating above xml file using Linq to Xml in C#. Here we are using XElement for root element Students inside that again using XElement for sub root element Student. Inside second root Student, we are using three times XElement from Name, Email and City element. Finally save the document for creating Student.xml file.

XDocument document = new XDocument(

            new XDeclaration("0.1","utf-8","yes"),

            new XElement("Students",

                new XElement("Student",

                    new XElement("Name","Jitesh"),

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

                    new XElement("City","Buxar")

                    )

                )

            );

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

Example 2

In this example, we will learn how to create below sample xml file using Linq to Xml.

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

Use below code for creating above sample xml file using Linq to Xml in C#. Here we are using XElement for root element Students inside that again using XElement for sub root element Student. In Student element, we are using XAttribute for Id attribute. Inside second root Student, we are using three times XElement from Name, Email and City. Finally save the document for creating Student.xml file.

XDocument document1 = new XDocument(

            new XDeclaration("0.1", "utf-8", "yes"),

            new XElement("Students",

                new XElement("Student",

                    new XAttribute("Id","1"),

                    new XElement("Name", "Jitesh"),

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

                    new XElement("City", "Buxar")

                    )

                )

            );

 

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

Example 3

In this example, we will learn how to create below sample xml file using Linq to Xml.

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

<Students>

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

</Students>

Use below code for creating above sample xml file using Linq to Xml in C#. Here we are using XElement for root element Students inside that again using XElement for Student element. In Student element, we are using four times XAttribute for Id, Name, Email and City attribute. Finally save the document for creating Student.xml file.

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

 

        XDocument document2 = new XDocument(

            new XDeclaration("0.1", "utf-8", "yes"),

            new XElement("Students",

                new XElement("Student",

                    new XAttribute("Id", "1"),

                    new XAttribute("Name", "Jitesh"),

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

                    new XAttribute("City", "Buxar")

                    )

                )

            );

 

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

Example 4

In this example, we will learn how to create below sample xml file using Linq to Xml.

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

Use below code for creating above sample xml file using Linq to Xml in C#. Here we are using XElement for root element Students inside that again using XElement for sub root element Student. Inside second sub root Student, we are using three times XElement from Name, Email and City element. Again using a XElement for sub root Education and inside that using three times XElement for Degree, Branch and University element. Finally save the document for creating Student.xml file.

XDocument document3 = new XDocument(

            new XDeclaration("0.1", "utf-8", "yes"),

            new XElement("Students",

 

                new XElement("Student",                   

                    new XElement("Name", "Jitesh"),

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

                    new XElement("City", "Buxar")

                    ),

 

                new XElement("Education",                   

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

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

                    new XElement("University", "RTU")

                    )

                )

            );

 

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

Example 5

In this example, we will learn how to create below sample xml file using Linq to Xml.

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

Use below code for creating above sample xml file using Linq to Xml in C#. Here we are using XElement for root element Students inside that again using XElement for sub root element Student. In Student element, we are using four times XElement from Name, Email, City and Education element. Again, Inside Education element using three times XElement for Degree, Branch and University element. Finally save the document for creating Student.xml file.

XDocument document4 = new XDocument(

            new XDeclaration("0.1", "utf-8", "yes"),

            new XElement("Students",

 

                new XElement("Student",                   

                    new XElement("Name", "Jitesh"),

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

                    new XElement("City", "Buxar"),

 

                    new XElement("Education",                   

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

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

                    new XElement("University", "RTU")

                    )

               )           

           )

        );

 

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



Please give your feedback for improving this page