对xml的操作C#增加和修改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsApplication1
{
public partial class xmldo : Form
{
public xmldo()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath.ToString() + "\\SystemConfig.xml");
XmlElement xm = doc.CreateElement("CONFIGINFO");
XmlElement xmLevel = doc.CreateElement("Level");
xmLevel.InnerText = "界面";
XmlElement xmCode = doc.CreateElement("Code");
xmCode.InnerText = "界面";
XmlElement xmName = doc.CreateElement("Name");
xmName.InnerText = "界面";
XmlElement xmValue = doc.CreateElement("Value");
xmValue.InnerText = "界面";
xm.AppendChild(xmLevel);
xm.AppendChild(xmCode);
xm.AppendChild(xmName);
xm.AppendChild(xmValue);

doc.DocumentElement.AppendChild(xm);
//XmlNode xn = doc.SelectSingleNode("DataSet/CONFIGINFO");
//xn.CloneNode(true);
//xn["Level"].InnerText = "界面";
//xn["Code"].InnerText = "WebServiceAddr1";
//xn["Name"].InnerText = "服务器地址";
//xn["Value"].InnerText = "http://192.168.3.210:9080/EDAServiceProject/services/ServiceEngine";
//doc.DocumentElement.AppendChild(xn);
doc.Save(Application.StartupPath.ToString() + "\\SystemConfig.xml");
Application.Exit();

}

private void button2_Click(object sender, EventArgs e)
{
//XmlDocument doc = new XmlDocument();
//doc.Load(Application.StartupPath.ToString() + "\\SystemConfig.xml");
DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath.ToString() + "\\SystemConfig.xml");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (Convert.ToString(ds.Tables[0].Rows[i]["Code"]) == "Password")
{
ds.Tables[0].Rows[i]["Level"] = "密码mimam";
ds.Tables[0].Rows[i]["Name"] = "数据库连接密码123";
ds.Tables[0].Rows[i]["Value"] = "密码deviceinfodeviceinfo";
}
}
ds.WriteXml(Application.StartupPath.ToString() + "\\SystemConfig.xml");

//XmlNodeList xnl = doc.SelectSingleNode("DataSet/CONFIGINFO/Code").ParentNode.ChildNodes;

//doc.Save(Application.StartupPath.ToString() + "\\SystemConfig.xml");
Application.Exit();
}
}
}

移动设备的文件路径的问题

//获得应用的路径
string fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;
string fullAppPath = Path.GetDirectoryName(fullAppName);
//DataSet ds = new DataSet();
//ds.ReadXml(fullAppPath + "\\" + "SystemConfig.xml");

DataSet ds = new DataSet();
ds.ReadXml(fullAppPath + "\\" + "SystemConfig.xml");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (Convert.ToString(ds.Tables[0].Rows[i]["Code"]) == "Password")
{
ds.Tables[0].Rows[i]["Level"] = "密码mimam";
ds.Tables[0].Rows[i]["Name"] = "数据库连接密码123";
ds.Tables[0].Rows[i]["Value"] = "密码deviceinfodeviceinfo";
}
}
ds.WriteXml(fullAppPath + "\\" + "SystemConfig.xml");
ds.Clear();
ds.ReadXml(fullAppPath + "\\" + "SystemConfig.xml");
this.dataGrid1.DataSource = ds.Tables[0];
//MessageBox.Show(fullAppPath + "\\" + "SystemConfig.xml");

你可能感兴趣的:(C++,c,xml,应用服务器,C#)