<?xml version="1.0" encoding="GB2312"?>
<root>
<log>
<Content><![CDATA[CustomerID,CompanyName,ContactName,1,AA,A1,2,BB,B1,3,CC,C1,4,DD,D1]]></Content>
</log>
</root> 如何取出CDATA中的数据,到到DATASET中!
LINQ中有这样的操作,但是我是2005,// Create the text file.
string csvString = @"GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA
HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA
LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA
LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA";
File.WriteAllText("cust.csv", csvString);// Read into an array of strings.
string[] source = File.ReadAllLines("cust.csv");
XElement cust = new XElement("Root",
from str in source
let fields = str.Split(',')
select new XElement("Customer",
new XAttribute("CustomerID", fields[0]),
new XElement("CompanyName", fields[1]),
new XElement("ContactName", fields[2]),
new XElement("ContactTitle", fields[3]),
new XElement("Phone", fields[4]),
new XElement("FullAddress",
new XElement("Address", fields[5]),
new XElement("City", fields[6]),
new XElement("Region", fields[7]),
new XElement("PostalCode", fields[8]),
new XElement("Country", fields[9])
)
)
);
Console.WriteLine(cust);
<root>
<log>
<Content><![CDATA[CustomerID,CompanyName,ContactName,1,AA,A1,2,BB,B1,3,CC,C1,4,DD,D1]]></Content>
</log>
</root> 如何取出CDATA中的数据,到到DATASET中!
LINQ中有这样的操作,但是我是2005,// Create the text file.
string csvString = @"GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA
HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA
LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA
LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA";
File.WriteAllText("cust.csv", csvString);// Read into an array of strings.
string[] source = File.ReadAllLines("cust.csv");
XElement cust = new XElement("Root",
from str in source
let fields = str.Split(',')
select new XElement("Customer",
new XAttribute("CustomerID", fields[0]),
new XElement("CompanyName", fields[1]),
new XElement("ContactName", fields[2]),
new XElement("ContactTitle", fields[3]),
new XElement("Phone", fields[4]),
new XElement("FullAddress",
new XElement("Address", fields[5]),
new XElement("City", fields[6]),
new XElement("Region", fields[7]),
new XElement("PostalCode", fields[8]),
new XElement("Country", fields[9])
)
)
);
Console.WriteLine(cust);
解决方案 »
- System.Configuration.ConfigurationManager.AppSettings 取值的问题
- 水晶报表加载图片时失真
- 子类能继承父类的构造函数?
- 如何获取动态生成的Table中的TextBox中的值
- 求助:FTP服务端源代码
- 可不可以从datatable中选出某一列不重复的数据。
- 关于ServicePointManager的ConnectionLimit设置问题
- C#中如何得到当前屏幕的尺寸大小呀。在线等。
- 请问window2000自带的smtp怎么使用?
- 请问,如何确保dataset中记录按照某一列是没有重复的?(分给正确的)
- .NET技术群(上海站):1910102 一起探讨探讨吧
- 函数参数较多时怎么办?
以下是button click event中的code,用来执行当文件选取了之后读取文件的内容。
System.Web.HttpPostedFile input = Request.Files[0];
if (input != null && input.ContentLength != 0)
{
string path = input.FileName.ToString();
System.IO.StreamReader reader = new System.IO.StreamReader(path);
reader.Peek();
while (reader.Peek() > 0)
{
string str = reader.ReadLine();
string[] split = str.Split(',');
if (split[0] != "" && split[1] != "")
{
System.Data.DataSet ds = new DataSet();
System.Data.DataRow dr = ds.Tables[0].NewRow();
dr[0] = split[0];
dr[1] = split[1];
ds.Tables[0].Rows.Add(dr);
}
}
}
把csv文件中的内容读到dataset里面。你应该看得懂。
下面是用C#写的一个简单的读写CSV文件的类。using System;
using System.Collections.Generic;
using System.IO;
using System.Text;namespace CSVDemo
{
/// <summary>
/// CSVUtil is a helper class handling csv files.
/// </summary>
public class CSVUtil
{
private CSVUtil()
{
}
//write a new file, existed file will be overwritten
public static void WriteCSV(string filePathName,List<String[]>ls)
{
WriteCSV(filePathName,false,ls);
}
//write a file, existed file will be overwritten if append = false
public static void WriteCSV(string filePathName,bool append, List<String[]> ls)
{
StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
foreach(String[] strArr in ls)
{
fileWriter.WriteLine(String.Join (“,",strArr) );
}
fileWriter.Flush();
fileWriter.Close();
}
public static List<String[]> ReadCSV(string filePathName)
{
List<String[]> ls = new List<String[]>();
StreamReader fileReader=new StreamReader(filePathName);
string strLine="";
while (strLine != null)
{
strLine = fileReader.ReadLine();
if (strLine != null && strLine.Length>0)
{
ls.Add(strLine.Split(','));
//Debug.WriteLine(strLine);
}
}
fileReader.Close();
return ls;
}
}
}
如何使用这个类可以看源代码。CSVDemo.rar (12.76 KB , 下载:1108次)
源代码演示了:
1.listview控件,openFileDialog控件的简单运用;
2.autoseed的Random类的使用;
3.保存CSV文件;
4.读取CSV文件;
5.简单的分层思想,视图-listview,业务数据-data,永久数据-csv file
本代码不涉及:
1.listview控件的复杂控制
2.CSV文件内容合法性检验,例如每行是否有相同的列。
Update
2007-12-14 一个不简单的处理csv文件的C#类(库)
http://www.codeproject.com/KB/database/CsvReader.aspx
<?xml version="1.0" encoding="GB2312"?>
<root>
<log>
<Content> <![CDATA[CustomerID,CompanyName,ContactName,1,AA,A1,2,BB,B1,3,CC,C1,4,DD,D1]]> </Content>
</log>
</root>
CDATA内容没有换行标志,标准CSV内容打开的时候,是换行的,另外如何把CDATA中的内容取出.