创建xml文件和向里面存储数据

前端之家收集整理的这篇文章主要介绍了创建xml文件和向里面存储数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


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);

}

}

原文链接:https://www.f2er.com/xml/299121.html

猜你在找的XML相关文章