1.首先确认需要创建的XML文件的路径和文件名称,如下:
string file = "D\\Sample.xml";
2.确认XML文件输出格式及编写版本:
XmlTextWriter xmlWrite = new XmlTextWriter(file, Encoding.UTF8);
xmlWrite.Formatting = Formatting.Indented;
xmlWrite.WriteStartDocument();
3.确认xml最外层节点名称:
xmlWrite.WriteStartElement("Sample");
4.输入你想创建的内容,如果内容较多可以用FOR循环,我这里已经先把要保存的内容存到list里面,所以直接用For循环:
for (int i = 0; i < Global.DIAlarmSVIDList.Count(); i++)
{
xmlWrite.WriteStartElement("Item");
xmlWrite.WriteAttributeString("ID", List[i].id.ToString());
xmlWrite.WriteAttributeString("ID1", List[i].id1.ToString());
.......
xmlWrite.WriteEndElement();//写完一条要记得这条语句,下次循环会重启新一行
}
xmlWrite.WriteEndElement();//xml编写结束
xmlWrite.Close();//关闭xml