xml文件本身也是文本文件,写这类文件,首先你要分析清楚xml文件的结构,然后用拼串的方式来生成xml串并输出即可。 读入xml文件可以使用System.Xml命名空间下的XMLDocument等类库来完成。
xml常用方法:
定义xml文档:XmlDocument xmlDoc=new XmlDocument();
初始化xml文档:xmlDoc.Load("文件路径");
创建根元素:XmlElemen xmlElemen=xmlDoc.CreateElemen("","Empoloyees","");
创建节点:XmlElement xesub1=xmlDoc.CreateElemen("title");
查找Empoloyees节点:XmlNode root=xmlDoc.selectSingleNode("Empoloyees");
添加节点:xe1.AppendChild(xesub1);
更改节点的属性:xe.SetAttribute("Name","小明");
移除xe的ID属性:xe.RemoveAttribute("ID");
删除节点title:xe.RemoveChild(xe2);
创建xml文档
public void CreateXMLDocument()
{
XmlDocument xmlDoc = new XmlDocument();
//加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmlDeclar;
xmlDeclar = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmlDoc.AppendChild(xmlDeclar);
//加入Employees根元素
XmlElement xmlElement = xmlDoc.CreateElement("", "Employees", "");
xmlDoc.AppendChild(xmlElement);
//添加节点
XmlNode root = xmlDoc.SelectSingleNode("Employees");
XmlElement xe1 = xmlDoc.CreateElement("Node");
xe1.SetAttribute("Name", "李明");
xe1.SetAttribute("学号", "123456789");
//添加子节点
XmlElement xeSub1 = xmlDoc.CreateElement("title");
xeSub1.InnerText = "学习VS";
xe1.AppendChild(xeSub1);
XmlElement xeSub2 = xmlDoc.CreateElement("price");
xe1.AppendChild(xeSub2);
XmlEleme
}