xml导出到csv的问题 [英] problems for exporting xml to csv

查看:123
本文介绍了xml导出到csv的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对我的文件转换项目有很多回应,这对我来说非常有用.但是同时我在将xml转换为csv时遇到了错误.运行我的项目后,文件是excel格式的,而不是csv.将这个xml的错误获取到csv.plz即可清除.下面已附加了代码.我也愿意编辑该xml文件.plz可以帮助我.
//default.aspx.cs//

Hi,


I got lot of responses for my file conversion project.It is very useful for me.But at the same time i got the error while converting xml to csv.After running my project the file is in excel format but not csv.Then I am getting the error for this xml to csv.plz clear it.Below i had attached the code.I am also willing to edit that xml file.plz help me.
//default.aspx.cs//

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         DataSet ds=new DataSet();
        ds.ReadXml( "C:/Documents and Settings/sridharan/Desktop/sridharan/exporting               
        xml values into excel or csv/XMLFile.xml");
        GridView1.DataSource =ds;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=gurunanak.csv;");
        Response.Charset = "";
        Response.ContentType = "application/text";

        GridView1.AllowPaging = false;
        GridView1.DataBind();

        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < GridView1.Columns.Count; k++)
        {
            //add separator
            sb.Append(GridView1.Columns[k].HeaderText + ',');
        }
       
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
              
                sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind(); 
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
       
    }

   
}



//default.aspx//



//default.aspx//

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
            Style="left: 124px; position: relative; top: 21px" AutoGenerateColumns="False" 
           
           >
            <footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
            <rowstyle backcolor="Aqua" forecolor="Fuchsia" />
            <editrowstyle backcolor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center" />
            <HeaderStyle BackColor="#FF9900" Font-Bold="True" ForeColor="#CC00FF" />
            <alternatingrowstyle backcolor="White" forecolor="#284775" />
            <columns>
                <asp:BoundField HeaderText="id" DataField="id" />
                <asp:BoundField HeaderText="name" 
                    DataField="name" ItemStyle-HorizontalAlign="Right" >
                 
<itemstyle horizontalalign="Right"></itemstyle>
                
                   <asp:BoundField HeaderText="phone" DataField="phone" />
                    <asp:BoundField HeaderText="address" DataField="address" />
                     <asp:BoundField HeaderText="age" DataField="age" />
                      <asp:BoundField HeaderText="dateofbirth" DataField="dateofbirth" />
                       <asp:BoundField HeaderText="company" DataField="company" />
            </columns> 
        
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    
    </form>
</body>
</html>


//xmlfile.xml


//xmlfile.xml

<employee>
  
    <id>1</id>
    <name>Florian</name>
    <phone>123</phone>
    <address>mumbai</address>
    <age>32</age>
    <dateofbirth>8.8.78</dateofbirth>
    <company>skysoft</company>
    
  
  
    <id>2</id>
    <name>Andreas</name>
    <phone>234</phone>
    <address>bengaluru</address>
    <age>21</age>
    <dateofbirth>7.7.85</dateofbirth>
    <company>Infotechsolutions</company>
  
  
    <id>3</id>
    <name>Martin</name>
    <phone>345</phone>
    <address>hyderabad</address>
    <age>47</age>
    <dateofbirth>29.3.87</dateofbirth>
    <company>vayana</company>
  
  
    <id>4</id>
    <name>jacobs</name>
    <phone>111</phone>
    <address>kolkatta</address>
    <age>22</age>
    <dateofbirth>2.2.88</dateofbirth>
    <company>brainmagic</company>
  
  
    <id>5</id>
    <name>Ricky</name>
    <phone>222</phone>
    <address>trichy</address>
    <age>55</age>
    <dateofbirth>4.6.99</dateofbirth>
    <company>Infosys</company>
  
  
    <id>6</id>
    <name>michael</name>
    <phone>333</phone>
    <address>chennai</address>
    <age>28</age>
    <dateofbirth>31.7.91</dateofbirth>
    <company>Photon</company>
  
  
    <id>7</id>
    <name>vauchan</name>
    <phone>444</phone>
    <address>chennai</address>
    <age>55</age>
    <dateofbirth>5.5.77</dateofbirth>
    <company>OFS</company>
  
  
    <id>8</id>
    <name>shane</name>
    <phone>555</phone>
    <address>chennai</address>
    <age>33</age>
    <dateofbirth>23.5.67</dateofbirth>
    <company>Sunsmart</company>
  
  
    <id>9</id>
    <name>watson</name>
    <phone>666</phone>
    <address>chennai</address>
    <age>44</age>
    <dateofbirth>18.9.85</dateofbirth>
    <company>Ascendas</company>
  
  
    <id>10</id>
    <name>Marsh</name>
    <phone>777</phone>
    <address>chennai</address>
    <age>22</age>
    <dateofbirth>1.1.1</dateofbirth>
    <company>sap</company>
  
  
    <id>11</id>
    <name>sridharan</name>
    <phone>888</phone>
    <address>chennai</address>
    <age>31</age>
    <dateofbirth>22.4.66</dateofbirth>
    <company>Wipro</company>
  
  
    <id>12</id>
    <name>mishra</name>
    <phone>999</phone>
    <address>Delhi</address>
    <age>23</age>
    <dateofbirth>1.1.99</dateofbirth>
    <company>Infosys</company>
  
  
    <id>13</id>
    <name>vaibhav</name>
    <phone>1000</phone>
    <address>Ranchi</address>
    <age>22</age>
    <dateofbirth>11.2.98</dateofbirth>
    <company>TCS</company>
  
  
    <id>14</id>
    <name>Rajesh</name>
    <phone>1100</phone>
    <address>kochin</address>
    <age>11</age>
    <dateofbirth>29.3.78</dateofbirth>
    <company>CTS</company>
  
</employee>

推荐答案

hi
您可以点击以下链接获取解决方案..

使用XSL将XML转换为CSV [
hi
YOu can follow the below link for the solution..

Convert XML to CSV, with XSL[^]


Regards
Ravi


这篇关于xml导出到csv的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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