C# : XML
XML读取到DataTable
1 public DataTable ReadIP() 2 { 3 DataTable dt = new DataTable(); 4 XmlDocument xmlDoc = new XmlDocument(); 5 string fullPath = Application.StartupPath;
string path = fullPath.Substring(0, fullPath.Length - 9) + @"Data\IPAndWebAdress.xml";
6 xmlDoc.Load(path); 7 XmlNode node = xmlDoc.SelectSingleNode("Adress"); 8 dt.Columns.Add("IP"); 9 foreach (XmlElement xe in node) 10 { 11 DataRow dr =dt.NewRow(); 12 dr["IP"] = xe.InnerText.Trim(); 13 dt.Rows.Add(dr); 14 } 15 return dt; 16 }
XML写入磁盘
1 public void Write(string web,string ip) 2 { 3 XmlDocument xmlDoc = new XmlDocument(); 4 string fullpath = Application.StartupPath;
string path = fullPath.Substring(0, fullPath.Length - 9) + @"Data\IPAndWebAdress.xml"; 5 xmlDoc.Load(path); 6 7 XmlNode node = xmlDoc.SelectSingleNode("Adress"); 8 XmlElement xe = xmlDoc.CreateElement("web"); 9 xe.SetAttribute("adress", web); 10 xe.InnerText = ip; 11 node.AppendChild(xe); 12 xmlDoc.Save(path); 13 }

浙公网安备 33010602011771号