Xml文件的操作总共有两套Api XmlDocument和XDocument,以下范例中包括将泛型集合写入到Xml文件中的范例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace Xml_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 递归写入TreeView的方法
/ <summary>
/ 读取Xml文件
/ </summary>
/ <param name = "sender" ></ param >
/ < param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
#region // 将xml文件(XDocument)加载到TreeView上
//1.1加载
XDocument document = XDocument.Load("*.Xml");
//2获取根节点
XElement rootElement = document.Root;
//3.将xml的根元素加载到TreeView的根节点上
TreeNode rootNode = treeView1.Nodes.Add(rootElement.Name.ToString());
//4.递归加载
LoadXmlToTreeView(rootElement, rootNode.Nodes);
#endregion
#region 通过XmlDocument将Xml加载到TreeView上
1.加载Xml文件到对象
XmlDocument document = new XmlDocument();
将xml文件加载到Dom对象上
document.Load("*.xml");
获得xml的根节点
XmlElement rootElement = document.DocumentElement;
将xml的根元素加载到Treeview上
TreeNode rootNode = treeView1.Nodes.Add(rootElement.Name);
实现递归将xml加载到Treeview中
LoadXmlToTreeViewByXmlDocument(rootElement, rootNode.Nodes);
#endregion
}
private void LoadXmlToTreeViewByXmlDocument(XmlElement rootElement, TreeNodeCollection treeNodeCollection)
{
try
{
循环rootElement下所有的子元素加载到
foreach (XmlNode item in rootElement.ChildNodes)
{
if (item.NodeType == XmlNodeType.Element)
{
如果当前节点是一个元素的子元素,把当前节点加载到TreeView上
TreeNode node = treeNodeCollection.Add(item.Name);
递归调用
LoadXmlToTreeViewByXmlDocument((XmlElement)item, node.Nodes);
}
else if (item.NodeType == XmlNodeType.Text || item.NodeType == XmlNodeType.CDATA)
{
treeNodeCollection.Add(item.InnerText);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void LoadXmlToTreeView(XElement rootElement, TreeNodeCollection treeNodeCollection)
{
获取rootElement下的所有子元素,并进行遍历
foreach (XElement item in rootElement.Elements())
{
if (item.Elements().Count() == 0)
{
treeNodeCollection.Add(item.Name.ToString()).Nodes.Add(item.Value);
}
else
{
将当前子节点加载到Treeview节点的集合中
TreeNode node = treeNodeCollection.Add(item.Name.ToString());
LoadXmlToTreeView(item, node.Nodes);
}
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
#region 通过编程方式实现xml写入
//c创建Dom对象
XmlDocument xmlDoc = new XmlDocument();
//1.0创建描述
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDeclaration);
//1.1创建根节点
XmlElement rootElement = xmlDoc.CreateElement("school");
xmlDoc.AppendChild(rootElement);
//1.2为根节点增加子元素
XmlElement xmlClassElement = xmlDoc.CreateElement("class");
XmlAttribute attr = xmlDoc.CreateAttribute("id");
attr.Value = "c01";
//1.3为class增加一个名字叫id的属性
xmlClassElement.Attributes.Append(attr);
rootElement.AppendChild(xmlClassElement);
//1.4位class元素增加一个student节点
XmlElement xmlstudentElement = xmlDoc.CreateElement("student");
//1.5为student元素增加sid属性
XmlAttribute xmlstudentAttribute = xmlDoc.CreateAttribute("sid");
xmlstudentAttribute.Value = "s011";
xmlstudentElement.Attributes.Append(xmlstudentAttribute);
xmlClassElement.AppendChild(xmlstudentElement);
XmlElement xmlNameElement = xmlDoc.CreateElement("Name");
xmlNameElement.InnerText = "崔德超";
XmlElement xmlAgeElement = xmlDoc.CreateElement("age");
xmlAgeElement.InnerText = "34";
xmlstudentElement.AppendChild(xmlNameElement);
xmlstudentElement.AppendChild(xmlAgeElement);
//保存xml文件
xmlDoc.Save("school.xml");
#endregion
#region 把list集合写入到xml文件
List<Person> list = new List<Person>();
list.Add(new Person() { Name = "1", Age = 1, Email = "123@qq.com" });
list.Add(new Person() { Name = "2", Age = 2, Email = "1234@qq.com" });
list.Add(new Person() { Name = "3", Age = 3, Email = "12345@qq.com" });
//1.创建一个Dom对象
XmlDocument xmlDoc = new XmlDocument();
//2.编写文档定义
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDec);
//3.编写一个根节点
XmlElement xmlroot = xmlDoc.CreateElement("List");
xmlDoc.AppendChild(xmlroot);
foreach (var item in list)
{
//创建一个person元素
XmlElement xmlPersonElement = xmlDoc.CreateElement("Person");
XmlAttribute xmlAttributeID = xmlDoc.CreateAttribute("id");
xmlAttributeID.Value = item.Name;
xmlPersonElement.Attributes.Append(xmlAttributeID);
XmlElement xmlNameElement = xmlDoc.CreateElement("Name");
xmlNameElement.InnerText = item.Name;
xmlPersonElement.AppendChild(xmlNameElement);
XmlElement xmlAgeElement = xmlDoc.CreateElement("Age");
xmlAgeElement.InnerText = item.Age.ToString();
xmlPersonElement.AppendChild(xmlAgeElement);
XmlElement xmlEmailElement = xmlDoc.CreateElement("Email");
xmlEmailElement.InnerText = item.Email;
xmlPersonElement.AppendChild(xmlEmailElement);
xmlroot.AppendChild(xmlPersonElement);
}
xmlDoc.Save("List.xml");
#endregion
#region 通过XDocument方式写入Xml文件
List<Person> list = new List<Person>();
list.Add(new Person() { Name = "1", Age = 1, Email = "123@qq.com" });
list.Add(new Person() { Name = "2", Age = 2, Email = "1234@qq.com" });
list.Add(new Person() { Name = "3", Age = 3, Email = "12345@qq.com" });
//1.创建一个Dom对象
XDocument xDoc = new XDocument();
XDeclaration xDec = new XDeclaration("1.0", "utf-8", "yes");
//设置xml文档定义
xDoc.Declaration = xDec;
//2.创建根节点
XElement rootElement = new XElement("List");
xDoc.Add(rootElement);
//3.循环List集合创建Person节点
for (int i = 0; i < list.Count; i++)
{
//为每个Person对象增加一个Person元素
XElement xPersonElement = new XElement("Person");
xPersonElement.SetAttributeValue("id", (i + 1).ToString());
xPersonElement.SetElementValue("Name", list[i].Name);
xPersonElement.SetElementValue("Age", list[i].Age);
xPersonElement.SetElementValue("Email", list[i].Email);
rootElement.Add(xPersonElement);
}
//4.保存到文件
xDoc.Save("newxml.xml");
#endregion
}
}
}
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
}