用C#写XML类型的配置文件:
1.首先要知道xml的格式标准。<?xml version="1.0" encoding="GB2312"?> 。 <?表示指令的开始;xml声明版本为1.0,编码类型
2. C#写xml配置文件
用到system.xml类库中的 XmlTextWriter类。例如下面:
XmlTextWriter lXmlWriter = new XmlTextWriter(lFileName, null);-----------------------------------------新建写xml的对象lXmlWriter lXmlWriter.Formatting = Formatting.Indented;-----------------------------------------------------格式化xml文件开头部分,必须有 lXmlWriter.WriteStartDocument();-------------------------------------------------------WriteStartDocument开始写xml文件,必须有 lXmlWriter.WriteStartElement("BandConfig");----------------------------------------第一个WriteStartElement(),是写的根节点
for (int i = 0; i < mConfigs.Count(); ++i) { //节点开始 lXmlWriter.WriteStartElement("Band");--------------------------------------------再次写WriteStartElement()就是根节点的子节点了 lXmlWriter.WriteElementString("BandWidth",XmlConvert.ToString(mConfigs[i].BandWidth));子节点中的元素WriteElementString lXmlWriter.WriteElementString("BandColor",(mConfigs[i].PenColor.ToArgb().ToString())); lXmlWriter.WriteElementString("BandLine",XmlConvert.ToString(mConfigs[i].PenWidth)); //节点结束 lXmlWriter.WriteEndElement();----------与lXmlWriter.WriteStartElement("Band")对应。,必须有 } lXmlWriter.WriteEndElement();------------与 lXmlWriter.WriteStartElement("BandConfig")对应,必须要 lXmlWriter.Close();---------------------------与lXmlWriter.WriteStartDocument()对应,必须有
|
请发表评论