且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用asp.net c#将xml文件转换为html表

更新时间:2023-09-27 20:40:04

写你的xml就像

 <   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 >







然后使用子键解析..


我建​​议使用xslt来执行此操作。





请看链接:

http://www.w3schools.com/xsl/xsl_transformation.asp [ ^ ]



Xml转换



 使用 System.Xml; 
使用 System.Xml.XPath;
使用 System.Xml.Xsl;
< br />

XPathDocument myXPathDoc = new XPathDocument(xml文件路径);
XslTransform myXslTrans = new XslTransform();
myXslTrans.Load(xsl文件路径);
XmlTextWriter myWriter = new XmlTextWriter( result .html null );
myXslTrans.Transform(myXPathDoc, null ,myWriter);





祝你好运!


 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 > \\\\ n);
try
{
XDocument xDocument = XDocument.Parse(xml);
XElement root = xDocument.Root;

var xmlAttributeCollection = root.Elements()。Attributes();


foreach(root.Elements()中的var ele)
{
if(!ele.HasElements)
{
string elename =;
html.Append(< tr &GT; 跨度>);

elename = ele.Name.ToString();

if(ele.HasAttributes)
{
IEnumerable < xattribute > attribs = ele.Attributes();
foreach(属性中的XAttribute attrib)
elename + = Environment.NewLine + attrib.Name.ToString()+
=+ attrib.Value.ToString();
}

html.Append(< table >
< tbody > < tr > < > < tbody > < tr > < td > + elename +< / td > < / tr > < / tbody > < / table > < / tr > < / tbody > < / table &GT; 跨度>);
html.Append(< table > < tbody > < tr > < td > + ele .Value +< / td > < / tr > < / tbody > < / table &GT; 跨度>);
html.Append(< / xattribute > < / tr &gt ; 跨度>);
}
else
{
string elename =;
html.Append(< tr &GT; 跨度>);

elename = ele.Name.ToString();

if(ele.HasAttributes)
{
IEnumerable < xattribute > attribs = ele.Attributes();
foreach(属性中的XAttribute attrib)
elename + = Environment.NewLine + attrib.Name.ToString()+=+ attrib.Value.ToString();
}

html.Append(< table >
< tbody > < tr > < > < tbody > < tr > < td > + elename +< / td > < / tr > < / tbody > < / table > < / tr > < / tbody >
< / table &GT; 跨度>);
html.Append(< table > < tbody > < tr > < td > + ConvertXmlToHtmlTable (ele.ToString())+< / td > < / tr &gt ; < / tbody > < / tab le > );
html.Append(< / xattribute > < / tr &gt ; 跨度>);
}
}

html.Append(< /表 > );
}
catch(例外e)
{
return xml;
//返回原始字符串,包含错误。
}
返回html.ToString();
}



in此flImport是fileuploader,在btUpload中是按钮,在click事件中它将XML文件转换为HTML表格,你可以查看结果在CKeditor。


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:

<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.

write ur xml like
<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 ..


I would suggest using xslt to do this.


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

Xml Transformation

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!


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.