Transfer XML file to another XML using XSLT in ASP.net


In this article I'm trying to explain how to transfer xml file to another xml using XSLTransformation. Using this we learn how to transfer xml file information into another xml. This article will help you those who are beginners to perform the same.

Transfer XML file to another XML using XSLT in ASP.net:



In this article I'm trying to explain how to transfer xml file to another xml using XSLTransformation. Using this we learn how to transfer xml file information into another xml.

Follow the below steps to achieve your goal.

step-1:



Open Visual studio and then create a new project and name it as XMLTransform.

step-2:



Right click on project and then choose Add-NewItem and then choose xml, give a name for that and then redesign the xml file as like below.

<?xml version="1.0" encoding="utf-8" ?>
<Company>
<Employee>
<Emp_Id>1</Emp_Id>
<Ename>naveen</Ename>
<Address>
<Address-1>D.No:5/3, Sriram nagar, Ramapuram</Address-1>
<Address-2></Address-2>
<State>Tamilnadu</State>
<City>Chennai</City>
<PinCode>600089</PinCode>
<Country>INDIA</Country>
</Address>
<Salary>10000</Salary>
<Remarks></Remarks>
<ReportingPerson>KrishnaKumar</ReportingPerson>
</Employee>
<Employee>
<Emp_Id>2</Emp_Id>
<Ename>syam</Ename>
<Address>
<Address-1>D.No:4-188,Sivaji colony,</Address-1>
<Address-2>Bapulapadu Mandal</Address-2>
<State>Andhra Pradesh</State>
<City>Hanuman Junction</City>
<PinCode>521105</PinCode>
<Country>INDIA</Country>
</Address>
<Salary>10000</Salary>
<Remarks></Remarks>
<ReportingPerson>Karthi</ReportingPerson>
</Employee>
</Company>



step-3:


once xml file has been created then right click on project section and then choose Add NewItem and add XSLTFile to the project, and give a name for that and redesign the XSLT file as like below.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

<xsl:output method="xml" indent="yes"/>

<xsl:template match="Company">
<Company>
<xsl:for-each select="Employee">
<Employee>
<xsl:copy-of select="Emp_Id"/>
<xsl:copy-of select="Ename"/>
<xsl:for-each select="Address">
<Address>
<xsl:copy-of select="Address-1"/>
<xsl:copy-of select="Address-2"/>
<xsl:copy-of select="State"/>
<xsl:copy-of select="City"/>
<xsl:copy-of select="PinCode"/>
<xsl:copy-of select="Country"/>
</Address>
</xsl:for-each>
<xsl:copy-of select="Salary"/>
<xsl:copy-of select="Remarks"/>
<xsl:copy-of select="ReportingPerson"/>

</Employee>
</xsl:for-each>
</Company>

</xsl:template>
</xsl:stylesheet>


step-4:



In the above transmission file if we have multiple nodes against the particular node then we use for-each to iterate the child nodes.

step-5:



once the transmission file has been created then right click on project section and then add new webform to the project and give a name for that and wrote below lines of code under page load event of a page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.IO;
using System.Xml.Xsl;

public partial class XmlTransform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XslTransform myXslTransform;
myXslTransform = new XslTransform();
string xslt = Server.MapPath("XSLTFile.xslt");
string xml=Server.MapPath("XMLFile.xml");
string outxml=Server.MapPath("outxml.xml");
myXslTransform.Load(xslt);
myXslTransform.Transform(xml, outxml);

Response.Write("File Transfered");
}


}


step-6:



now execute the page and then see the output xml file in the specified location and see the result.

Conclusion:



This article will help you for beginners those who are trying to transfer xml to xml using XSLTransfer in ASP.net.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: