public static void insertData(string id,string name,string password) {
XmlDocument xml = new XmlDocument();
xml.Load(@"E:\a.xml");
XmlNode root = xml.SelectSingleNode("User");
XmlElement User = xml.CreateElement("user");
User.SetAttribute("id",id);
XmlElement Name = xml.CreateElement("name");
Name.InnerText = name;
XmlElement Password = xml.CreateElement("password");
Password.InnerText = password;
root.AppendChild(User);
User.AppendChild(Name);
User.AppendChild(Password);
xml.Save(@"E:\a.xml");
}
public static void createXml(string filePath) {
if (!File.Exists(filePath))
{
FileStream fs = File.Create(filePath);
fs.Flush();
fs.Close();
FileInfo fi = new FileInfo(filePath);
StreamWriter sw = fi.AppendText();
string description = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> ";
sw.WriteLine(description);
sw.WriteLine("<User></User>");
sw.Flush();
sw.Close();
}
}
static void Main(string[] args)
{
createXml(@"E:\a.xml");
for(int i=0;i<5;i++){
insertData(i.ToString(),"name"+i,"ppp"+i);
}
}