XmlTextWriter myWriter = new XmlTextWriter(文件路径,null);
//写XML 的文档版本号1.0
myWriter.WriteStartDocument();
//写开始元素
myWriter.WriteStartElement("root");
//写元素
myWriter.WriteStartElement("a");
myWriter.WriteString("Tale");
myWriter.WriteEndElement();
myWriter.WriteStartElement("b");
myWriter.WriteString("Th");
myWriter.WriteEndElement();
myWriter.WriteStartElement("c");
myWriter.WriteString("T3");
myWriter.WriteEndElement();
//写结束文档
myWriter.WriteEndDocument();
//关闭流且保存文件中去
myWriter.Close();
2.xml的读:
XmlReader xmlr = XmlReader.Create(文件路径);
xmlr.MoveToContent();
xmlr.Read();
if (xmlr.Name.ToString() == "databasename")
{
databaseName= xmlr.ReadInnerXml().ToString();
//顺序读其他元素
.........
}
另外:
XmlReader xmlr = XmlReader.Create("ftp_set.xml");//打开要读取的文档
xmlr.MoveToContent();
while (xmlr.Read()) //循环读取
{
switch (xmlr.Name.ToString())
{
case "uri": //xml标签
this.strFTPURI = xmlr.ReadInnerXml().ToString();
break;
case "potnumber":
strPotNum = xmlr.ReadInnerXml().ToString();
break;
case "username":
this.strUserName = xmlr.ReadInnerXml().ToString();
break;
case "password":
this.strPassword = xmlr.ReadInnerXml().ToString();
break;
}
}
3.注意:xml读的时候如果文件内有空格和回车,一次读将读空格和回车。可以编程忽略掉这些空格和回车。