你这些问题都是xml操作问题,去msdn上看xmldocument的使用
解决方案 »
- 200分求一个C#接收邮件的代码
- C# 大数据量代码性能优化
- 救命啊!!!
- C# WINFORM 如果动态写入资源文件
- 弹出窗口和父窗口的值传递问题。
- web程序中,datagrid控件DataKeyField属性能否指定多个字段?
- 要写段windows服务的多线程程序, 在两台完全一样的服务器上运行, 要访问同一个数据库, 如何保证执行的唯一性
- c#2005 serialport 串口通讯问题
- 新手问题!各位大侠帮帮我~~~走过,路过不能错过
- 用什么方法可以得到字符串的ASCII值?
- 类的层级结构太深,用override能显示可重写的方法,但是怎么知道这个重写的方法到底是在那个父类里定义的.
- 关于多线程与委托问题
第二种情况是,我通过SQL查询出一个xml类型数据列,把这列已第一列不同导出不同的XML文件。
比如上面这个图片,有两个运单号码:2986257,2995974对应不同的商品代码,那么导出后应该有两个xml文件
2986257.xml和2995974.xml里面明细就是商品代码。
this.dgv1.DataSource = c_function.getds(sql).Tables[0];
求例子啊,我不会自己写,我只会看着别人的代码修修补补。新手,才开始学习C#
我这里用listbox,如果直接点dataGridView1里运单号,代码要修改一下 //新建一个datatable用于保存读入的数据
//给datatable添加三个列
dt.Columns.Add("运单号", typeof(String));
dt.Columns.Add("商品代码", typeof(String));
dt.Columns.Add("商品名称", typeof(String));
//读入文件
DataSet ds=new DataSet();
using (StreamReader reader = new StreamReader("1.txt", Encoding.Default))
{
//循环读取所有行
while (!reader.EndOfStream)
{
//将每行数据,用-分割成3段
string[] data = reader.ReadLine().Split(' ');//每行数据之间有一个空格
//新建一行,并将读出的数据分段,分别存入对应的列中,有几列建立几个
DataRow dr = dt.NewRow();
dr[0] = data[0];
dr[1] = data[1];
dr[2] = data[2];
//将这行数据加入到datatable中
dt.Rows.Add(dr);
}
}
//将datatable绑定到datagridview上显示结果
this.dataGridView1.DataSource = dt; // DataTable dt=(DataTable)this.datagridview.DataSource;
for(int i=0;i<dataGridView1.RowCount-1;i++)
{
if (!listBox3.Items.Contains(dt.Rows[i]["运单号"].ToString()))
{
listBox3.Items.Add(dt.Rows[i]["运单号"].ToString());
}
}
private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
DataRow[] drs = dt.Select("运单号 like '%" + listBox3.SelectedItem.ToString().Trim() + "%'");
DataSet ds = new DataSet();
DataTable newdt=ToDataTable1(drs);
newdt.TableName=("newtable");
newdt.WriteXml(listBox3.SelectedItem.ToString().Trim()+".xml");
MessageBox.Show("ok");
}
还要加上这个:将取出的多行值放到新表中,
public DataTable ToDataTable1(DataRow[] rows)
{
if (rows == null || rows.Length == 0) return null;
DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
foreach (DataRow row in rows)
tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
return tmp;
}
this.dataGridView1.DataSource = dt;
这句子以上的代码是我在dataGridView1里做一些数据的,你可以用,
这个代码是数据绑定到dataGridView1后,放到后面,让运单号显示在listBox3里
for(int i=0;i<dataGridView1.RowCount-1;i++)
{
if (!listBox3.Items.Contains(dt.Rows[i]["运单号"].ToString()))
{
listBox3.Items.Add(dt.Rows[i]["运单号"].ToString());
}
}
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim cnn As New SqlConnection
cnn.ConnectionString = "server=192.168.1.2;database=data;user id=sa;password=sa"
cnn.Open()
Dim ds As New DataSet
Dim da As New SqlDataAdapter("select * from table", cnn)
cnn.Close()
da.Fill(ds, "table")
If ds.Tables(0).Rows.Count <> 0 Then
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
Dim id As String = ds.Tables(0).Rows(i)("CorpOrderID").ToString()
Dim xml As String = ds.Tables(0).Rows(i)("xmlCol").ToString()
writexml(id, xml)
Next
End If
End Sub
Public Sub writexml(ByVal id As String, ByVal xml As String)
Dim sw As StreamWriter = New StreamWriter(Application.StartupPath + "\" + id + ".xml", False, Encoding.UTF8)
Try
sw.WriteLine(xml)
sw.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Dim id As String = ds.Tables(0).Rows(i)("CorpOrderID").ToString()
Dim xml As String = ds.Tables(0).Rows(i)("xmlCol").ToString()
2、writexml 将取得的数据写到以ID为文件名的文件里,
//按id不同形成不同的文件,文件内容是XML
public void writexml(string id,string xml)
{
StreamWriter sw =new StreamWriter(Application.StartupPath + @"\" + id + ".xml");
try
{
sw.WriteLine(xml);
sw.Close();
}
catch (Exception ex )
{
MessageBox.Show(ex.Message);
}
}
private void button18_Click(object sender, EventArgs e)
{
SqlConnection cnn=new SqlConnection();
cnn.ConnectionString = @"server=192.168.1.2;database=data;user id=sa;password=sa";
cnn.Open();
DataSet ds =new DataSet();
SqlDataAdapter da=new SqlDataAdapter("select * from table", cnn);
cnn.Close();
da.Fill(ds, "table");
if(ds.Tables[0].Rows.Count==0)
{
for(int i=0;i<ds.Tables[0].Rows.Count - 1;i++)
{
string id = ds.Tables[0].Rows[i]["CorpOrderID"].ToString();
string xml = ds.Tables[0].Rows[i]["xmlCol"].ToString();
writexml(id, xml);
}
} }
是的,新建的文件都放在EXE的目录下
public void CreateXML(string FileName, string XMLContent)
{
//文件保存目录
string DataPath = "F:\\";
//判断文件是否已经存在,如果存在即覆盖
if (File.Exists(DataPath + FileName))
{
File.Delete(DataPath + FileName);
}
FileStream objFileStream = new FileStream(DataPath + FileName, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(objFileStream, Encoding.Default);
try
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(XMLContent);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sw.Flush();
sw.Close();
}
}
FileStream objFileStream = new FileStream(DataPath + FileName, FileMode.Create, FileAccess.Write);调用:例子
private void button20_Click(object sender, EventArgs e)
{
CreateXML("aaa.xml", "<birthday><NO1><type>类型</type><date>时间</date><title>信息</title><name>姓名</name></NO1><NO2><type>类型</type><date>时间</date><title>信息</title><name>姓名</name></NO2></birthday>");
}
string DataPath = "F:\\";你这里就将文件在f根目录下输出了,你保存后会在f:\下找到XML文件,
if(ds.Tables[0].Rows.Count==0) 都等于0了哪里来的记录啊。要是if(ds.Tables[0].Rows.Count!=0)才对
这下文件倒是建立了,名字也是对的,但是里面的内容是空的,什么都没有。