var result = from x in item.Descendants("GCP") select new { label = x.Attribute("label").Value, alt = x.Attribute("alt").Value, lat = x.Attribute("lat").Value, lng = x.Attribute("lng").Value, x = x.Attribute("x").Value, y = x.Attribute("y").Value }; dataGridView1.DataSource = result.ToList(); 这样就可把读取的数据都显示出来 了
string MyFileName = this.openFileDialog.FileName;
if (MyFileName.Trim() == "")
return; if (File.Exists(MyFileName))
{
string[] str = File.ReadAllLines(MyFileName, Encoding.Unicode);
}读取到数组str了……然后呢……?
下面代码是写入文件的操作;
文件路径是你要写入的文件路径;
FileStream fs = new FileStream("文件路径", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(message);
sw.Close();
fs.Close();
DataSet ds = new DataSet();
ds.ReadXml("ntest.xml");
StreamWriter sw = new StreamWriter("ntest.txt");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string s1 = ds.Tables[0].Rows[i][0].ToString();
string s2 = ds.Tables[0].Rows[i][1].ToString();
string s3 = ds.Tables[0].Rows[i][2].ToString();
string s4 = ds.Tables[0].Rows[i][3].ToString();
sw.WriteLine(s1+" "+s2+" "+s3+" "+s4);
}
sw.Close();
ds.Tables[0]--> ds.Tables["GCP"]
“System.Data.DuplicateNameException”类型的未经处理的异常在 System.Data.dll 中发生
其他信息: 无法添加名为“index”的列: 此 DataTable 中已存在同名的嵌套表。
什么原因呢?谢谢!
http://pan.baidu.com/s/1jGyyYKu
http://pan.baidu.com/s/1c0AM7o0
谢谢帮忙查看……
换用方法:因dataset读取时,你文件中有2个index,所以出错了, XDocument item = XDocument.Load("tingkou2.xml");
StreamWriter sw = new StreamWriter("ntest.txt");
var result = from x in item.Descendants("GCP")
select new
{
label = x.Attribute("label").Value,
alt = x.Attribute("alt").Value,
lat = x.Attribute("lat").Value,
lng = x.Attribute("lng").Value,
x = x.Attribute("x").Value,
y = x.Attribute("y").Value };
foreach (var el in result)
{
//MessageBox.Show(el.label + " " + el.alt + " " + el.lat);
sw.WriteLine(el.label + " " + el.alt + " " + el.lat+" "+el.lng+" "+el.x+" "+el.y);
}
sw.Close();
我想读取完了之后展示其中一部分数据的话,
需要用到什么控件呢?winform中!
var result = from x in item.Descendants("GCP")
select new
{
label = x.Attribute("label").Value,
alt = x.Attribute("alt").Value,
lat = x.Attribute("lat").Value,
lng = x.Attribute("lng").Value,
x = x.Attribute("x").Value,
y = x.Attribute("y").Value };
dataGridView1.DataSource = result.ToList();
这样就可把读取的数据都显示出来 了