LINQ to XML Tutorial
Lesson 7 of 8 · Part 7 Transform one XML format to another XML format using linq to xml · Database
Part 7 Transform one XML format to another XML format using linq to xml
Text version of the video
http://csharp-video-tutorials.blogspot.com/2014/08/part-7-transform-one-xml-format-to.html
Slides
http://csharp-video-tutorials.blogspot.com/2014/08/part-7-transform-one-xml-format-to_31.html
LINQ to SQL Tutorial – All Text Articles & Slides
http://csharp-video-tutorials.blogspot.com/2014/08/linq-to-xml-tutorial.html
LINQ to XML Tutorial Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
convert xml to different format, xml tranformation, dot net, .net, asp.net, c#, linq to xml tutorial
Part 7 – Transform one XML format to another XML format 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:DemoDemoData.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:DemoDemoResult.xml”);