Suggested Videos
Part 3 - Querying xml document using linq to xml
Part 4 - Modifying xml document using linq to xml
Part 5 - Transforming XML to CSV using LINQ to XML
In Part 5 of LINQ to XML tutorial, we discussed, how to tranform XML to CSV.
In this video, we will discuss transforming XML to HTML table.
We want to tranform the following XML to HTML
After transformation, data in the HTML file should look as shown below.
Code to transform XML to HTML
Part 3 - Querying xml document using linq to xml
Part 4 - Modifying xml document using linq to xml
Part 5 - Transforming XML to CSV using LINQ to XML
In Part 5 of LINQ to XML tutorial, we discussed, how to tranform XML to CSV.
In this video, we will discuss transforming XML to HTML table.
We want to tranform the following XML to HTML
<?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>
After transformation, data in the HTML file should look as shown below.
<table border="1">
<thead>
<tr>
<th>Country</th>
<th>Name</th>
<th>Gender</th>
<th>TotalMarks</th>
</tr>
</thead>
<tbody>
<tr>
<td>USA</td>
<td>Mark</td>
<td>Male</td>
<td>800</td>
</tr>
<tr>
<td>USA</td>
<td>Rosy</td>
<td>Female</td>
<td>900</td>
</tr>
<tr>
<td>India</td>
<td>Pam</td>
<td>Female</td>
<td>850</td>
</tr>
<tr>
<td>India</td>
<td>John</td>
<td>Male</td>
<td>950</td>
</tr>
</tbody>
</table>
Code to transform XML to HTML
XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");
XDocument result = new XDocument
(new XElement("table", new XAttribute("border", 1),
new XElement("thead",
new XElement("tr",
new XElement("th", "Country"),
new XElement("th", "Name"),
new XElement("th", "Gender"),
new XElement("th", "TotalMarks"))),
new XElement("tbody",
from student in xmlDocument.Descendants("Student")
select new XElement("tr",
new XElement("td", student.Attribute("Country").Value),
new XElement("td", student.Element("Name").Value),
new XElement("td", student.Element("Gender").Value),
new XElement("td", student.Element("TotalMarks").Value)))));
result.Save(@"C:\Demo\Demo\Result.htm");
No comments:
Post a Comment
It would be great if you can help share these free resources