XmlDocument创建xml文件
通过XmlDocument创建Dom对象操作xml文件(.net2.0)
操作案例1:
//1,内存中构建一个Dom对象
XmlDocument xmlDocument = new XmlDocument();
//增加一个文档说明
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDocument.AppendChild(xmlDeclaration);
//为文档添加一个根节点
//2.1创建一个根元素
XmlElement xmlRootElement = xmlDocument.CreateElement("school");
xmlDocument.AppendChild(xmlRootElement);
//3,为根元素增加子元素,都要加载root元素节点下
XmlElement xmlClassElement = xmlDocument.CreateElement("class");
//为class元素增加一个id属性
XmlAttribute classIdAttr = xmlDocument.CreateAttribute("id");
classIdAttr.Value = "c01";
xmlClassElement.Attributes.Append(classIdAttr);
xmlRootElement.AppendChild(xmlClassElement);
//4,为class创建student节点
XmlElement xmlStudentElement = xmlDocument.CreateElement("student");
XmlAttribute stuIdAttr = xmlDocument.CreateAttribute("sid");
stuIdAttr.Value = "s0101";
xmlStudentElement.Attributes.Append(stuIdAttr);
//student节点追加到class节点下
xmlClassElement.AppendChild(xmlStudentElement);
//5,向student节点下添加一个name节点
XmlElement xmlNameElement = xmlDocument.CreateElement("name");
xmlNameElement.InnerText = "张三";
XmlElement xmlAgeElement = xmlDocument.CreateElement("age");
xmlAgeElement.InnerText = "19";
xmlStudentElement.AppendChild(xmlNameElement);
xmlStudentElement.AppendChild(xmlAgeElement);
//2,将该Dom对象写入到xml文件中
xmlDocument.Save("../../xml/school1.xml");
下面是生成的文件school1.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<school>
<class id="c01">
<student sid="s0101">
<name>张三</name>
<age>19</age>
</student>
</class>
</school>
操作案例2:
public static void ListConvertToXML()
{
List<Person> list = GetPersons();
//1,创建Dmo对象
XmlDocument xdoc = new XmlDocument();
//2,编写文档声明
XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xdoc.AppendChild(xdec);
//3,编写一个根节点
XmlElement xmlRootNode = xdoc.CreateElement("List");
xdoc.AppendChild(xmlRootNode);
//4,循环创建Person节点
for (int i = 0; i < list.Count; i++)
{
//4.1创建一个person元素
XmlElement xmlPersonNode = xdoc.CreateElement("Person");
//4.2给person创建一个id属性
XmlAttribute xmlAttrId = xdoc.CreateAttribute("id");
xmlAttrId.Value = (i + 1).ToString();
xmlPersonNode.Attributes.Append(xmlAttrId);
//4.3给Person增加子节点-Name
XmlElement xmlNameNode = xdoc.CreateElement("Name");
xmlNameNode.InnerText = list[i].Name;
xmlPersonNode.AppendChild(xmlNameNode);
//4.3给Person增加子节点-Age
XmlElement xmlAgeNode = xdoc.CreateElement("Age");
xmlAgeNode.InnerText = list[i].Age.ToString();
xmlPersonNode.AppendChild(xmlAgeNode);
//4.3给Person增加子节点-Email
XmlElement xmlEmailNode = xdoc.CreateElement("Email");
xmlEmailNode.InnerText = list[i].Email.ToString();
xmlPersonNode.AppendChild(xmlEmailNode);
//最后把person加到根节点下
xmlRootNode.AppendChild(xmlPersonNode);
}
//保存文件
xdoc.Save("../../xml/Person.xml");
}
public static List<Person> GetPersons()
{
List<Person> list = new List<Person>
{
new Person{ Name="张三", Age=17,Email="zhangsan123@qq.com"},
new Person{ Name="李四", Age=18,Email="lisi123@qq.com"},
new Person{ Name="王五", Age=19,Email="wangwu123@qq.com"},
new Person{ Name="赵六", Age=20,Email="zhaoliu123@qq.com"},
new Person{ Name="刘备", Age=21,Email="liubei123@qq.com"}
};
return list;
}
生成的Person.xml文件
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<List>
<Person id="1">
<Name>张三</Name>
<Age>17</Age>
<Email>zhangsan123@qq.com</Email>
</Person>
<Person id="2">
<Name>李四</Name>
<Age>18</Age>
<Email>lisi123@qq.com</Email>
</Person>
<Person id="3">
<Name>王五</Name>
<Age>19</Age>
<Email>wangwu123@qq.com</Email>
</Person>
<Person id="4">
<Name>赵六</Name>
<Age>20</Age>
<Email>zhaoliu123@qq.com</Email>
</Person>
<Person id="5">
<Name>刘备</Name>
<Age>21</Age>
<Email>liubei123@qq.com</Email>
</Person>
</List>
XDocument创建xml文件
通过XDocument创建Dom对象操作xml文件(.net2.0以上的版本)
public static void ListConvetrToXMLHeigh()
{
List<Person> list = GetPersons();
//1,创建一个Dmo对象
XDocument xDoc = new XDocument();
//2,创建一个文档声明
XDeclaration xDeclaration = new XDeclaration("1.0", "utf-8", null);
// xDoc.Add(xDeclaration);//写法错误
xDoc.Declaration = xDeclaration;
//3,创建一个根节点
XElement rootNode = new XElement("List");
xDoc.Add(rootNode);
//4,创建根节点
for (int i = 0; i < list.Count; i++)
{
//4.1每个person对象创建节点,添加到根节点下
XElement personNode = new XElement("Person");
personNode.SetAttributeValue("id", (i + 1).ToString());
//4.2给person节点添加子节点
personNode.SetElementValue("Name", list[i].Name);
personNode.SetElementValue("Age", list[i].Age);
personNode.SetElementValue("Email", list[i].Email);
//4.3追加写入根节点下
rootNode.Add(personNode);
}
xDoc.Save("../../xml/person2.xml", SaveOptions.None);
}
public static List<Person> GetPersons()
{
List<Person> list = new List<Person>
{
new Person{ Name="张三", Age=17,Email="zhangsan123@qq.com"},
new Person{ Name="李四", Age=18,Email="lisi123@qq.com"},
new Person{ Name="王五", Age=19,Email="wangwu123@qq.com"},
new Person{ Name="赵六", Age=20,Email="zhaoliu123@qq.com"},
new Person{ Name="刘备", Age=21,Email="liubei123@qq.com"}
};
return list;
}
生成的person2.xml
<?xml version="1.0" encoding="utf-8"?>
<List>
<Person id="1">
<Name>张三</Name>
<Age>17</Age>
<Email>zhangsan123@qq.com</Email>
</Person>
<Person id="2">
<Name>李四</Name>
<Age>18</Age>
<Email>lisi123@qq.com</Email>
</Person>
<Person id="3">
<Name>王五</Name>
<Age>19</Age>
<Email>wangwu123@qq.com</Email>
</Person>
<Person id="4">
<Name>赵六</Name>
<Age>20</Age>
<Email>zhaoliu123@qq.com</Email>
</Person>
<Person id="5">
<Name>刘备</Name>
<Age>21</Age>
<Email>liubei123@qq.com</Email>
</Person>
</List>