很简单的读写功能,自己在空闲时做的demo。
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new System.Data.DataSet("MESSAGE");//父节点
DataTable table = new System.Data.DataTable("FeedBack");//子节点1
ds.Tables.Add(table);
table.Columns.Add("Model_Name", typeof(string));
table.Columns.Add("PRJ_Name", typeof(string));
table.Columns.Add("area_name", typeof(string));
table.Columns.Add("Major_Name", typeof(string));
DataRow row = table.NewRow();
row[0] = "阿大";
row[1] = "王二";
row[2] = "张三";
row[3] = "李四";
ds.Tables["FeedBack"].Rows.Add(row);//子节点1添加
DataTable table1 = new System.Data.DataTable("FeedBack1");//子节点2
ds.Tables.Add(table1);
table1.Columns.Add("Model_Name1", typeof(string));
table1.Columns.Add("PRJ_Name1", typeof(string));
table1.Columns.Add("area_name1", typeof(string));
table1.Columns.Add("Major_Name1", typeof(string));
DataRow row1 = table1.NewRow();
row1[0] = "阿大";
row1[1] = "王二";
row1[2] = "张三";
row1[3] = "李四";
ds.Tables["FeedBack1"].Rows.Add(row1);//子节点2添加
//判断XML文件是否存在
string name = "123.xml";
string path = ("E:/BIM_APP/" + name);
if (!File.Exists(path))
{
FileStream file = File.Create(path);//创建文件
file.Close();//关闭文件流
ds.WriteXml(path);
}
else
{
ds.WriteXml(path);
}
}
private void button2_Click(object sender, EventArgs e)
{
//将XML文件加载进来
XDocument document = XDocument.Load("E:\\BIM_APP\\123.xml");
//获取到XML的根元素进行操作
XElement root = document.Root;
XElement ele = root.Element("FeedBack1");
//获取name标签的值
XElement shuxing = ele.Element("Model_Name1");
Console.WriteLine(shuxing.Name);//获取Name值
Console.WriteLine(shuxing.Value);//获取value值
//获取根元素下的所有子元素
IEnumerable<XElement> enumerable = root.Elements();
foreach (XElement item in enumerable)
{
foreach (XElement item1 in item.Elements())
{
Console.WriteLine(item1.Name); //获取Name值
Console.WriteLine(item1.Value); //获取Value值
}
}
}
}