本文介绍了如何在不使用fileuploader的xslt的情况下将xml文件转换为c#中的html文件?在c#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中有一个ckeditor。上面那个ckeditor我有一个fileuploader控件我需要通过点击导入将xml文件上传到上传我需要将其内容转换为html并将该内容绑定到ckeditor

使用

ckeditor.Text =一些字符串;即htmlcontentinstring。

我编程很差。对于xml转换我没有使用xslt文件。

i很快就需要解决方案将它应用到我的项目中所以请帮助我

下面的代码使用的是XSLT文件它的工作完全没问题。我需要在不使用XSLT的情况下修改此代码。

i have a ckeditor in a page . above that ckeditor i have a fileuploader control i need to upload xml files to the uploader by clicking import i need to convert its contents to html and bind that content to ckeditor
using
ckeditor.Text= some string; ie htmlcontentinstring.
i am very poor in programming. for xml convertion i am not using xslt files.
i need the solution for this very soon to apply it in my project so please help me
The below code is using XSLT files and its working completely fine. i need to modify this code without using XSLT.

if (flImport.HasFile)
       {
           string returnHTML = string.Empty;
           string fileName = string.Empty;
           fileName = flImport.PostedFile.FileName;
           string flFilePath = Server.MapPath("~/Files/" + fileName);
           flImport.SaveAs(Server.MapPath("~/Files/" + fileName));
           string randomFileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
           string htmlFilePath = Path.Combine(Server.MapPath("HTML/"), randomFileName + ".html");
           string MyXmlPath = Request.PhysicalApplicationPath + "Files/" + fileName;
           string MyXsltPath = Request.PhysicalApplicationPath + "\\XmlFile.xslt";
           XPathDocument xmlDoc = new XPathDocument(MyXmlPath);
           XslCompiledTransform XSLTransform = new XslCompiledTransform();
           XSLTransform.Load(MyXsltPath);
           string MyHTMLPath = Request.PhysicalApplicationPath + "\\HtmlFile.htm";
           XSLTransform.Transform(MyXmlPath, MyHTMLPath);
           if (System.IO.File.Exists(MyHTMLPath))
           {
               returnHTML = System.IO.File.ReadAllText(MyHTMLPath);
               ckContent.Text = returnHTML;
           }

这是将xml文件转换为html并显示数据。但有时我们不会有XSLT文件。所以我需要在不使用xslt的情况下转换xml文件。

有人可以帮助我吗?

in this it is converting an xml file to html and it is showing the data. But sometimes we wont be having XSLT files. so i need to convert xml files without using xslt.
Can somebody help me in this?

推荐答案


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();
        }



以上代码适用于我的项目。谢谢大家。


The above code works in my project. Thanks Everybody.


这篇关于如何在不使用fileuploader的xslt的情况下将xml文件转换为c#中的html文件?在c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:22