Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 7 - Transform one XML format to another XML format using linq to xml

Suggested Videos
Part 4 - Modifying xml document using linq to xml
Part 5 - Transforming XML to CSV using LINQ to XML
Part 6 - Transforming XML to HTML table using LINQ to XML



In Part 5, we discussed, tranforming XML to CSV
In Part 6, we discussed, tranforming XML to HTML

In this video, we will discuss transforming one XML format to another XML format



We want to tranform the following XML format to a different format
<?xml version="1.0" encoding="utf-8"?>
<Students>
  <Student Country="USA">
    <Name>Mark</Name>
    <Gender>Male</Gender>
    <TotalMarks>800</TotalMarks>
  </Student>
  <Student Country="USA">
    <Name>Rosy</Name>
    <Gender>Female</Gender>
    <TotalMarks>900</TotalMarks>
  </Student>
  <Student Country="India">
    <Name>Pam</Name>
    <Gender>Female</Gender>
    <TotalMarks>850</TotalMarks>
  </Student>
  <Student Country="India">
    <Name>John</Name>
    <Gender>Male</Gender>
    <TotalMarks>950</TotalMarks>
  </Student>
</Students>

The tranformed XML format should be as shown below.
<?xml version="1.0" encoding="utf-8"?>
<Students>
  <USA>
    <Student>
      <Name>Mark</Name>
      <Gender>Male</Gender>
      <TotalMarks>800</TotalMarks>
    </Student>
    <Student>
      <Name>Rosy</Name>
      <Gender>Female</Gender>
      <TotalMarks>900</TotalMarks>
    </Student>
  </USA>
  <India>
    <Student>
      <Name>Pam</Name>
      <Gender>Female</Gender>
      <TotalMarks>850</TotalMarks>
    </Student>
    <Student>
      <Name>John</Name>
      <Gender>Male</Gender>
      <TotalMarks>950</TotalMarks>
    </Student>
  </India>
</Students>

Code to transform XML to a different format
XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

XDocument result = new XDocument(
        new XElement("Students",
            new XElement("USA",
                from s in xmlDocument.Descendants("Student")
                where s.Attribute("Country").Value == "USA"
                select new XElement("Student",
                    new XElement("Name", s.Element("Name").Value),
                    new XElement("Gender", s.Element("Gender").Value),
                    new XElement("TotalMarks", s.Element("TotalMarks").Value))),
            new XElement("India",
                from s in xmlDocument.Descendants("Student")
                where s.Attribute("Country").Value == "India"
                select new XElement("Student",
                    new XElement("Name", s.Element("Name").Value),
                    new XElement("Gender", s.Element("Gender").Value),
                    new XElement("TotalMarks", s.Element("TotalMarks").Value)))));

result.Save(@"C:\Demo\Demo\Result.xml");

LINQ to XML tutorial

1 comment:

  1. Thank You Sir..:) Your videos are really helpful and crisp clear..:) I recommend your videos to almost everyone I know who is eager to learn .Net!

    I have re-written this example without hard-coding. All thanks to you..:)

    XDocument newDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("XML Doc without Hard-Coding"),
    new XElement("Students",
    from stu in students
    group stu by stu.Element("Country").Value into g
    select new XElement(g.Key,
    (from gx in g
    select new XElement("Student",
    new XElement("Id", gx.Attribute("Id").Value),
    new XElement("Name", gx.Element("Name").Value),
    new XElement("Gender", gx.Element("Gender").Value),
    new XElement("TotalMarks", gx.Element("TotalMarks").Value))))));

    Thanks once again.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.