protected void btUpload_Click(object sender, EventArgs e)
{
string fullFilePath = null;
if (flImport.HasFile)
{
string fileName = string.Empty;
string extension = string.Empty;
string path = string.Empty;
fileName = flImport.PostedFile.FileName;
string flFilePath = Server.MapPath("~/Files/" + fileName);
flImport.SaveAs(Server.MapPath("~/Files/" + fileName));
fullFilePath = flFilePath;
extension = Path.GetExtension(fileName);
path = fileName;
if (string.Equals(extension, ".xml") || string.Equals(extension, ".xslt"))
{
string xml= System.IO.File.ReadAllText(fullFilePath);
ckContent.Text = ConvertXmlToHtmlTable(xml);
}
}
plImport.Visible = false;
}
protected string ConvertXmlToHtmlTable(string xml)
{
StringBuilder html = new StringBuilder("<table align="center" hold=" /> " border="1" class="xmlTable">\r\n");
try
{
XDocument xDocument = XDocument.Parse(xml);
XElement root = xDocument.Root;
var xmlAttributeCollection = root.Elements().Attributes();
foreach (var ele in root.Elements())
{
if (!ele.HasElements)
{
string elename = "";
html.Append("<tr>");
elename = ele.Name.ToString();
if (ele.HasAttributes)
{
IEnumerable<xattribute> attribs = ele.Attributes();
foreach (XAttribute attrib in attribs)
elename += Environment.NewLine + attrib.Name.ToString() +
"=" + attrib.Value.ToString();
}
html.Append("<table><tbody><tr><table><tbody><tr><td>" + elename + "</td></tr></tbody></table></tr></tbody></table>");
html.Append("<table><tbody><tr><td>" + ele.Value + "</td></tr></tbody></table>");
html.Append("</xattribute></tr>");
}
else
{
string elename = "";
html.Append("<tr>");
elename = ele.Name.ToString();
if (ele.HasAttributes)
{
IEnumerable<xattribute> attribs = ele.Attributes();
foreach (XAttribute attrib in attribs)
elename += Environment.NewLine + attrib.Name.ToString() + "=" + attrib.Value.ToString();
}
html.Append("<table><tbody><tr><table><tbody><tr><td>" + elename + "</td></tr></tbody></table></tr></tbody></table>");
html.Append("<table><tbody><tr><td>" + ConvertXmlToHtmlTable(ele.ToString()) + "</td></tr></tbody></table>");
html.Append("</xattribute></tr>");
}
}
html.Append("</table>");
}
catch (Exception e)
{
return xml;
// Returning the original string incase of error.
}
return html.ToString();
}
in This flImport is the fileuploader and in btUpload is the button, in click event it is converting an XML file to HTML table and you can view the result it in CKeditor.