Monday, 20 April 2015

C# Translating from One XML Schema to Another

Input XML
<?xml version="1.0"?>
<investments>
<item type="stock" exch="nyse" symbol="ZCXM" company="zacx corp"
price="28.875"/>
<item type="stock" exch="nasdaq" symbol="ZFFX" company="zaffymat inc"
price="92.250"/>
<item type="stock" exch="nasdaq" symbol="ZYSZ" company="zysmergy inc"
price="20.313"/>
</investments>
view raw gistfile1.xml hosted with ❤ by GitHub
Translator
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
namespace XMLtoXML
{
public partial class Translate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Input XML
string strInputXMLPath = "C:\\input.xml";
//Conver XML to string
string strInputXML = File.ReadAllText(strInputXMLPath);
//strInputXML = strInputXML.Replace("&", "&amp;"); //Replace special charecters..
//Create XElement using XML string
File.WriteAllText("dummy.xml", strInputXML);
XElement inputXMLdummy = XElement.Load("dummy.xml");
//Create OutPut XML
//Create Output XML XElement
XDocument outPutDoc = new XDocument();
XElement outPutRoot = new XElement("portfolio");
outPutDoc.Add(outPutRoot);
//Read all nodes in dummy XML
IEnumerable<XElement> dummyXMLNodes =
from ele in inputXMLdummy.Elements()
select ele;
//Iterate each node in dummy XML
foreach (XElement element in dummyXMLNodes)
{
outPutRoot = OutPutXML(outPutRoot, element);
}
//Now we have out put xml in outPutDoc object
//Now save outPutDoc as XML
string strOutPutXMLPathwithName = "C:\\output.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
settings.OmitXmlDeclaration = false;
XmlWriter writer = XmlWriter.Create(strOutPutXMLPathwithName, settings);
outPutDoc.Save(writer);
writer.Close();
}
public static XElement OutPutXML(XElement outPutRoot, XElement inputElementNode)
{
var varInputElementNode = XElement.Parse(inputElementNode.ToString());
//Create new XElement for stock
XElement outputStockElement = new XElement("stock");
//Add exchange attribute for stock node, attribute name as exchange and value from input exch
outputStockElement.SetAttributeValue("exchange", varInputElementNode.Attribute("exch").Value);
//Add stock to portfolio (root)
outPutRoot.Add(outputStockElement);
//Create new XElement for name
XElement outputNameElement = new XElement("name");
//Set value for name
outputNameElement.SetValue(varInputElementNode.Attribute("company").Value);
//Add name to stock
outputStockElement.Add(outputNameElement);
//Create new XElement for symbol
XElement outputSymbolElement = new XElement("symbol");
//Set value for symbol
outputSymbolElement.SetValue(varInputElementNode.Attribute("symbol").Value);
//Add symbol to stock
outputStockElement.Add(outputSymbolElement);
//Create new XElement for price
XElement outputPriceElement = new XElement("price");
//Set value for price
outputPriceElement.SetValue(varInputElementNode.Attribute("price").Value);
//Add price to stock
outputStockElement.Add(outputPriceElement);
return outPutRoot;
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub
Output XML
<?xml version="1.0"?>
<portfolio>
<stock exchange="nyse">
<name>zacx corp</name>
<symbol>ZCXM</symbol>
<price>28.875</price>
</stock>
<stock exchange="nasdaq">
<name>zaffymat inc</name>
<symbol>ZFFX</symbol>
<price>92.250</price>
</stock>
<stock exchange="nasdaq">
<name>zysmergy inc</name>
<symbol>ZYSZ</symbol>
<price>20.313</price>
</stock>
</portfolio>
view raw gistfile1.xml hosted with ❤ by GitHub