使用asp.net c#将xml文件转换为html表 [英] Converting xml file into html table using asp.net c#

查看:74
本文介绍了使用asp.net c#将xml文件转换为html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将xml文件转换为asp.net C#中的html表。标签的内容应该插入到表的单元格中,请帮助.xml文件的部分如下:

< pre lang =HTML> < atul >
< financial_report scale = 10000000 >
< year_end_on date = 31-mar-2010 >

< net_sal es >
< gross_sales > 1226 < / gross_sales >
< less_ex_duty > 58 < / less_ex_duty >
< netsales > 1168 < / netsales >
< other_income > 30 < span class =code-keyword>< / other_income >
< total_net_sales > 1198 < span class =code-keyword>< / total_net_sales >
< / net_sales >
< 支出 >
< inc_dec_stock > (24)< / inc_dec_stock >
< consump > 663 < / consump >
< 购买 > 17 < / purchase >
< 员工 > 103 < / employees >
< pow_fu_wtr > 113 < / pow_fu_wtr >
< manuf > 69 < / manuf < span class =code-keyword>>
< 折旧 > 37 < /折旧 < span class =code-keyword>>
< 其他 > 110 < / others < span class =code-keyword>>
< total > 1088 &l t; / total >
< /支出 >
< profit_4m_opern > 110 < / profit_4m_opern >
< othr_income > 5 < / othr_income >
< prof_b4_intrst > 115 < / prof_b4_int rst >
< 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 >





提前谢谢你。

解决方案

写你的xml就像

 <   number1  >  1226 58 1168 30 1198 <   / number1  < span class =code-keyword>>  
< 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 > );

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 < span class =code-keyword>>
< tbody > < tr > < > < tbody > < tr > < td > + elename +< / td > < / tr > < / tbody > < / table > < / tr > < / tbody < span class =code-keyword>> < / 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)
elename + = Environment.NewLine + attrib.Name.ToString()+=+ attrib.Value.ToString();
}

html.Append(< table < span class =code-keyword>>
< tbody > < tr > < > < tbody > < tr > < td > + elename +< / td > < / tr > < / tbody > < / table > < / tr > < / tbody < span class =code-keyword>>
< / table > );
html.Append(< table > < tbody > < tr > < td > + ConvertXmlToHtmlTable (ele.ToString())+< / td > < / tr > ; < / tbody > < / tab le > );
html.Append(< / xattribute > < / tr > ; );
}
}

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.


这篇关于使用asp.net c#将xml文件转换为html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆