用c#写入xml的方法如下:
View Code
#region "生成XML"
// 创建一个表示所要生成的XML文件路径的字符串。如果该路径指向NTFS分区,则需要相关的访问权限。
fileName = "testHisMon.xml";
// 创建一个写入XML数据的文件流
FileStream myFileStream = new FileStream(fileName, System.IO.FileMode.Create);
// 使用文件流对象创建一个XmlTextWriter对象
XmlTextWriter writer = new XmlTextWriter(myFileStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
writer.WriteStartElement("OnlineMonXML");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xsi:noNamespaceSchemaLocation", "D:\\ming\\OnlineMon\\XMLData\\GDHB_OnlineMonXML.xsd");
writer.WriteAttributeString("version", "0.09.0005");
// OnlineMonData
writer.WriteStartElement("OnlineMonData");
// Stations
writer.WriteStartElement("Stations");
// Station
writer.WriteStartElement("Station");
writer.WriteAttributeString("strStationType", "污染源自动监测");
writer.WriteAttributeString("strAreaID", _strAreaID);
writer.WriteAttributeString("nStationID", _stationID);
writer.WriteAttributeString("strPassword", _strPassword);
// Points
writer.WriteStartElement("Points");
// Points
writer.WriteStartElement("Point");
writer.WriteAttributeString("strAreaID", _strAreaID);
writer.WriteAttributeString("nStationID", _stationID);
writer.WriteAttributeString("nPointID", pointID);
// MonDatas
writer.WriteStartElement("MonDatas");
// return;
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
myFileStream.Close();
#endregion