Click here to Skip to main content
15,886,742 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i convert xml file into html table in asp.net C#.The contents of tag should be inserted into the cells of the table please help.part of xml file is like:
HTML
<atul>
<financial_report scale="10000000">
<year_end_on date="31-mar-2010"> 
   
      <net_sales>
	     <gross_sales>1226</gross_sales>
         <less_ex_duty>58</less_ex_duty>
		 <netsales>1168</netsales>
         <other_income>30</other_income>
         <total_net_sales>1198</total_net_sales>
	  </net_sales> 
	  <expenditure>
          <inc_dec_stock>(24)</inc_dec_stock>
          <consump>663</consump>
          <purchase>17</purchase>
          <employees>103</employees>
          <pow_fu_wtr>113</pow_fu_wtr>
          <manuf>69</manuf>
          <depreciation>37</depreciation>
          <others>110</others>
		  <total>1088</total>
      </expenditure>  
	  <profit_4m_opern>110</profit_4m_opern>
	  <othr_income>5</othr_income>
	  <prof_b4_intrst>115</prof_b4_intrst>
	  <interst_expns>26</interst_expns>
	  <prof_los_b4_xchng>89</prof_los_b4_xchng>
	  <xchng_rate_diff>(9)</xchng_rate_diff>
	  <prof_los_4m_ordnry>80</prof_los_4m_ordnry>
	  <tax_expens>
	      <curr_tax>22</curr_tax>
		  <deferd_tax>5</deferd_tax>
		  <fring_ben_tax>0</fring_ben_tax>
		  <less_mat_cred>0</less_mat_cred>
		  <total_tax>27</total_tax>
	  </tax_expens>


thank you in advance.
Posted
Updated 8-Oct-21 7:12am
v3
Comments
koool.kabeer 27-Jul-10 7:44am    
GridView is Rendered as an HTML Table in a Web Page.....

Can You more clarify your need and your scenario......
Maciej Los 13-Oct-14 17:32pm    
What have you tried? Where are you stuck?
How should look destination format?

write ur xml like
HTML
<number1> 1226  58  1168  30  1198  </number1>
<number2>  (24)  663  17  103  113  69  37  110  1088   </number2>
<number3> 110  5  115  26  89  (9)  80   22  5  0  0  27  </number3>



Then parse it using subkey ..
 
Share this answer
 
v2
I would suggest using xslt to do this.


Look at the link:
http://www.w3schools.com/xsl/xsl_transformation.asp[^]

Xml Transformation

C#
using System.Xml;
using System.Xml.XPath;
using  System.Xml.Xsl;
<br />

XPathDocument myXPathDoc = new XPathDocument(xml file path);
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(xsl file path);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;


Good luck!
 
Share this answer
 
XML
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.
 
Share this answer
 
Comments
CHill60 13-Oct-14 19:24pm    
4 years late with this!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900