C# 对象的XML序列化,求大神

一个敲代码的骚年 2016-04-01 01:53:11
如题,我现在想要做一个软件,用DataGridView接收数据之后,可以用txt格式保存在项目根目录下,本人属于新手,最好是求参考案例,要全代码的最好,我在百度找了,代码有不少,可是我都不知道该在哪里写,求大神给个案例链接什么的,急用,谢谢!
...全文
303 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
安全命令 2016-04-01
  • 打赏
  • 举报
回复
纠正一下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace dg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
List<test> ts = new List<test>();
private void button1_Click(object sender, EventArgs e)
{
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
xmlResult.Append("<mytest>\r\n");
Random r = new Random();
DataGridView dg = dataGridView1;
for (int i = 0; i < dg.RowCount-1; i++) {
test t = new test();
t.Ces1 = dg.Rows[i].Cells[0].Value.ToString();
t.Ces2 = dg.Rows[i].Cells[1].Value.ToString();
ts.Add(t);
}
string txt = "";
string sPath = "D:\\ces\\";//存放的文件夹路径
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);//不存在创建文件夹
}
int s = 0;
foreach(test t in ts){

xmlResult.AppendFormat("<ces id=\"{0}\" category=\"{1}\">\r\n", s, "第" + s + "行");
xmlResult.AppendFormat("<ces1>{0}</ces1>\r\n", t.Ces1);
xmlResult.AppendFormat("<ces2>{0}</ces2>\r\n", t.Ces2);
xmlResult.Append("</ces>\r\n");
//txt += t.Ces1 + " " + t.Ces2 + "\r\n";
s++;
}
xmlResult.Append("</mytest>\r\n");
string name = r.Next(000000000, 999999999).ToString() + ".xml";
// 创建文件
FileStream fs = new FileStream(sPath + name, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
StreamWriter sw = new StreamWriter(fs); // 创建写入流
sw.WriteLine(xmlResult); // 写入文本
sw.Close(); //关闭文件

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
}
public class test {
string ces1;

public string Ces1
{
get { return ces1; }
set { ces1 = value; }
}
string ces2;

public string Ces2
{
get { return ces2; }
set { ces2 = value; }
}
}
}


结果
安全命令 2016-04-01
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace dg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        List<test> ts = new List<test>();
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
            xmlResult.Append("<mytest>\r\n");  
            Random r = new Random();
            DataGridView dg = dataGridView1;
            for (int i = 0; i < dg.RowCount-1; i++) {
                test t = new test();
                t.Ces1 = dg.Rows[i].Cells[0].Value.ToString();
                t.Ces2 = dg.Rows[i].Cells[1].Value.ToString();
                ts.Add(t);
            }
            string txt = "";
            string sPath = "D:\\ces\\";//存放的文件夹路径
            if (!Directory.Exists(sPath))
            {
                Directory.CreateDirectory(sPath);//不存在创建文件夹
            }
            int s = 0;
            foreach(test t in ts){

                xmlResult.AppendFormat("<ces id=\"{0}\" category=\"{1}\">\r\n", s, "第" + s + "行");
                xmlResult.AppendFormat("<ces1>{0}</title>\r\n", t.Ces1);
                xmlResult.AppendFormat("<ces2>{0}</title>\r\n", t.Ces2);
                xmlResult.Append("</ces>\r\n");  
                //txt += t.Ces1 + "  " + t.Ces2 + "\r\n";
                s++;
            }
            xmlResult.Append("</mytest>\r\n");  
           string name = r.Next(000000000, 999999999).ToString() + ".xml";
            // 创建文件
           FileStream fs = new FileStream(sPath + name, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
            StreamWriter sw = new StreamWriter(fs); // 创建写入流
            sw.WriteLine(xmlResult); // 写入文本
            sw.Close(); //关闭文件
        
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
   public class test {
        string ces1;

        public string Ces1
        {
            get { return ces1; }
            set { ces1 = value; }
        }
        string ces2;

        public string Ces2
        {
            get { return ces2; }
            set { ces2 = value; }
        }
    }
}

  • 打赏
  • 举报
回复
引用 3 楼 MOruilichun 的回复:
获取项目路径的方法别人的帖子 http://www.cnblogs.com/85538649/archive/2011/08/18/RootDirectory.html
可是我需要用到已节点方式保存啊,可以加我QQ聊吗?1154027575
安全命令 2016-04-01
  • 打赏
  • 举报
回复
你要的东西我已写好,度盘地址http://pan.baidu.com/s/1pLy37zt 请叫我雷锋

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace dg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        List<test> ts = new List<test>();
        private void button1_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            DataGridView dg = dataGridView1;
            for (int i = 0; i < dg.RowCount-1; i++) {
                test t = new test();
                t.Ces1 = dg.Rows[i].Cells[0].Value.ToString();
                t.Ces2 = dg.Rows[i].Cells[1].Value.ToString();
                ts.Add(t);
            }
            string txt = "";
            string sPath = "D:\\ces\\";//存放的文件夹路径
            if (!Directory.Exists(sPath))
            {
                Directory.CreateDirectory(sPath);//不存在创建文件夹
            }
            foreach(test t in ts){
                txt += t.Ces1 + "  " + t.Ces2 + "\r\n";
            }
           string name = r.Next(000000000, 999999999).ToString() + ".txt";
            // 创建文件
           FileStream fs = new FileStream(sPath + name, FileMode.OpenOrCreate, FileAccess.ReadWrite); //可以指定盘符,也可以指定任意文件名,还可以为word等文件
            StreamWriter sw = new StreamWriter(fs); // 创建写入流
            sw.WriteLine(txt); // 写入文本
            sw.Close(); //关闭文件
        
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
   public class test {
        string ces1;

        public string Ces1
        {
            get { return ces1; }
            set { ces1 = value; }
        }
        string ces2;

        public string Ces2
        {
            get { return ces2; }
            set { ces2 = value; }
        }
    }
}
安全命令 2016-04-01
  • 打赏
  • 举报
回复
获取项目路径的方法别人的帖子 http://www.cnblogs.com/85538649/archive/2011/08/18/RootDirectory.html
exception92 2016-04-01
  • 打赏
  • 举报
回复
用DataGridView接收数据之后,可以用txt格式保存在项目根目录下 -》 遍历DataGridVIew的数据源,拼接字段显示,使用StreamWriter类写入行。至于路径 找找C# Application相关的东西。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧