文章目录
1 使用XmlDocument创建XML文档2 使用XmlTextWriter写XML文档3 使用LINQ to XML 的XDocument类4 小结
本文介绍了在winform中使用C#开发语言来创建XML文档的三种方式,并介绍了各自的优缺点。
方法1是使用
XmlDocument创建XML文档,方法2是使用
XmlTextWriter编写XML文档,方法3是使用
LINQ to XML的Document类生成XML文档。
1 使用XmlDocument创建XML文档
使用XmlDocument.CreateXmlDeclaration来声明头部信息,用XmlDocument.CreateElement创建元素, 把创建的头部信息,还有元素添加到文档中,最后保存。元素之间有同级关系,也有父子关系,可以通过设定的Parent来决定两者之间的联系。
CreateXmlDeclaration(string version, string encoding, string standalone)
version: 版本必须写 1.0 encoding: 设置保存到文件的编码方式,默认是UTF-8 stangalone: 该值必须是“yes”或“no”.如果是null或String.empty,保存的时候不在XML声明上写这个特性。
//创建一个空的XML
XmlDocument document = new XmlDocument();
//声明头部
XmlDeclaration dec = document.CreateXmlDeclaration("1.0", "utf-8", "yes");
document.AppendChild(dec);
//创建根节点
XmlElement root = document.CreateElement("Citys");
document.AppendChild(root);
//创建父元素
XmlElement childEle1 = document.CreateElement("Province");
//属性
childEle1.SetAttribute("level", "发达");
//中间文本
childEle1.InnerText = "广东";
//添加到根节点
root.AppendChild(childEle1);
//创建子元素
XmlElement subchildEle1 = document.CreateElement("City");
//属性
subchildEle1.SetAttribute("level", "省会");
//中间文本
subchildEle1.InnerText = "广州";
//添加到父结点
childEle1.AppendChild(subchildEle1);
//创建子元素
XmlElement subchildEle2 = document.CreateElement("City");
subchildEle2.SetAttribute("level", "一线");
//中间文本
subchildEle2.InnerText = "深圳";
//添加到父结点
childEle1.AppendChild(subchildEle2);
//创建父元素
XmlElement childEle2 = document.CreateElement("Province");
//属性
childEle2.SetAttribute("level", "发展");
//中间文本
childEle2.InnerText = "广西";
//添加到根节点
root.AppendChild(childEle2);
//创建子元素
XmlElement subchildEle3 = document.CreateElement("City");
subchildEle3.SetAttribute("level", "三线");
//中间文本
subchildEle3.InnerText = "桂林";
//添加到父结点
childEle2.AppendChild(subchildEle3);
//保存文档
document.Save(@"D:\temp.xml");
运行结果
扩展添加注释和字符数字段
//评论注释
XmlComment comment = document.CreateComment("这是一份关于城市省份的文件");
root.AppendChild(comment);
//字符数字段
XmlCDataSection CData;
CData = document.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");
childEle2.AppendChild(CData);
运行结果
2 使用XmlTextWriter写XML文档
在XmlTextWriter 类中有一些函数是成对出现的,比如WriteStartDocument和WriteEndDocument; 元素WriteStartElement和WriteEndElement,用来标注一个元素的开始和结束,有很强的对应性。
//创建XmlTextWriter对象
XmlTextWriter textWriter = new XmlTextWriter(@"D:\writer.xml", Encoding.UTF8);
//xml文档开始
textWriter.WriteStartDocument();
//写根节点
textWriter.WriteStartElement("Citys");
//注释
textWriter.WriteComment("这是使用XmlTextWriter写的");
textWriter.WriteStartElement("Province");
textWriter.WriteAttributeString(null, "level", null, "发展");
textWriter.WriteString("广西");
//添加子项
textWriter.WriteStartElement("city");
textWriter.WriteAttributeString(null, "level", null, "省会");
textWriter.WriteString("南宁");
textWriter.WriteEndElement();
textWriter.WriteStartElement("city");
textWriter.WriteAttributeString(null, "level", null, "旅游");
textWriter.WriteString("桂林");
textWriter.WriteEndElement();
//结束Province广西
textWriter.WriteEndElement();
textWriter.WriteElementString("Province","广东");
//结束整个文档
textWriter.WriteEndDocument();
textWriter.Close();
运行结果
3 使用LINQ to XML 的XDocument类
使用到的命名空间
using System.Xml;
using System.Xml.Linq;
使用LinqTo Xml就很方便,XML文档层级结构多的话,写代码时要注意缩进方便阅读,示例代码:
XDocument document = new XDocument(
new XElement("Citys",
new XComment("这是使用Ling写的XML"),
new XElement("Province", new XText("广西"),
new XElement("city", new XText("南宁"),new XAttribute("level", "省会")),
new XElement("city", new XText("桂林"), new XAttribute("level", "旅游"))
),
new XElement("Province", new XText("广东"),
new XElement("city", new XText("广州"), new XAttribute("level", "省会")),
new XElement("city", new XText("深圳"), new XAttribute("level", "一线"))
)
)
);
document.Save(@"D:\Ling.xml");
运行结果:
4 小结
1 使用XmlDocument创建XML文档,创建的节点指定父节点,就构成层级关系,最后将最上层的节点添加到Document中保存即可。
2 使用XmlTextWriter编写XML文档时,该类有很强元素开始和结束的对应关系(l例如WriteStartElement和WriteEndElement),通过这种嵌套关系来建立元素之间的层级。
3 使用LINQ to XML构建XML文档时,很便利快捷,有种所见即所得的既视感,虽然也是通过嵌套建立的层级关系,但又不像XmlTextWriter的严格接口要求。如果层级比较多,需要规范的代码缩进。