如何在c#.net中将xml转换为csv [英] how to convert xml to csv in c# .net

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

问题描述

Hai to all,





我想在网络应用程序中将xml文件转换为csv格式。我该怎么办。我尝试了很多。但点击一个按钮后它直接进入excelsheet而不是csv.plz帮助我



下面是示例代码。

Hai to all,


I want to convert xml file into csv format in web application.How can i do.I tried lot.But after clicking a button it going straightly into excelsheet only but not csv.plz help me

Below is the sample code.

 //sample.xml
  <authors_table>
  <authors>
    <au_id>172-32-1176</au_id>
    <au_lname>White</au_lname>
    <au_fname>Johnson</au_fname>
    <phone>408 496-7223</phone>
    <address>10932 Bigge Rd.</address>
    <city>Menlo Park</city>
    <state>CA</state>
    <zip>94025</zip>
    <contract>true</contract>
  </authors>
<authors_table></authors_table></authors_table>





输出为

172-32-1176,white,johnson,408 496-7223,10932 Bigge Rd。,Menlo Park,CA,94025,true



output as
172-32-1176,white,johnson,408 496-7223,10932 Bigge Rd.,Menlo Park,CA,94025,true

推荐答案

如果我理解正确的话,你已经有了将xml转换为csv的实现,对吗?



你的意思是你的输出csv文件在Excel中被打开了吗?原因是您有用于打开设置为Excel的csv文件的默认程序。
if I understand correctly, you already have the implementation to convert xml to csv, right?

Do you mean that your output csv file gets opened up in Excel? The reason for that is that you have default program for opening csv files set to Excel.


您永远不会转换它。无论你怎么做,你都会丢失一些信息。



-SA
You never "convert" it. No matter how you do it you loose some information.

—SA


以下方法完全满足你的要求。

因为我遇到了同样的问题,碰巧写了下面的代码来得到我需要的东西。



这将DataSet转换为XML和然后你可以看到text / csv。您可以使用其他几种ContentType来获取所需内容。



XML文件:



The following method would exactly suffice your requirement.
As I faced the same issue and happen to write the following code to get what I needed.

This converts the DataSet to XML and then to text/csv as you can see. There are few other ContentTypes that you may use to get what''s required.

XML File:

<NewTable>
     <Table>
          <a1>12345</a1>
          <a2>12345</a2>
          <a3>12345</a3>
          <a4>12345</a4>
          <a5>12345</a5>
     </Table>
</NewTable>





< b>功能:





Function:

protected string ConvertToCSV(DataSet objDataSet)
{
        string xmlInput = objDataSet.GetXml();
        string csvOut = string.Empty;
        XDocument doc = XDocument.Parse(xmlInput);
        StringBuilder sb = new StringBuilder(100000);
        foreach (XElement node in doc.Descendants("Table"))
        {
            foreach (XElement innerNode in node.Elements())
            {
                //"{0}," give you the output in Comma seperated format.
                sb.AppendFormat("{0},", innerNode.Value);
            }
            sb.Remove(sb.Length - 1, 1);
            sb.AppendLine();
        }
        csvOut = sb.ToString();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = "text/csv";
        Response.ContentEncoding = Encoding.GetEncoding(0);
        Response.AppendHeader("Content-Disposition", "attachment;filename=Report.csv");
        Response.Output.Write(csvOut);
        Response.End();
        return csvOut;
}


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

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