سلام دوست خوب،
اینم یک نمونه:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ConnectionString>
<DataSource>.\SQLEXPRESS</DataSource>
<InitialCatalog>Your_Db</InitialCatalog>
<UserID>
</UserID>
<Password>
</Password>
<IntegratedSecurity>True</IntegratedSecurity>
<Security>False</Security>
</ConnectionString>
</Configuration>
protected internal void GetConnectionString()
{
try
{
if (File.Exists(_path))
{
XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(_path));
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
XmlDocument doc = new XmlDocument();
doc.Load(vr);
vr.Close();
foreach (XmlElement element in doc.SelectNodes("//ConnectionString"))
{
_DataSource = element.ChildNodes[0].InnerText;
_InitialCatalog = element.ChildNodes[1].InnerText;
_UserID = element.ChildNodes[2].InnerText;
_Password = element.ChildNodes[3].InnerText;
_IntegratedSecurity = Convert.ToBoolean(element.ChildNodes[4].InnerText);
_Security = Convert.ToBoolean(element.ChildNodes[5].InnerText);
}
}
}
catch { }
}
private void SaveConnection(object sender, EventArgs e)
{
try
{
string path = "Configuration.xml";
XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader(path));
vr.ValidationType = ValidationType.None;
vr.EntityHandling = EntityHandling.ExpandEntities;
XmlDocument doc = new XmlDocument();
doc.Load(vr);
vr.Close();
foreach (XmlElement element in doc.SelectNodes("//ConnectionString"))
{
XmlNode root = doc.DocumentElement["ConnectionString"];
//Replace the DataSource Element Of XML Document
XmlElement elemDataSource = doc.CreateElement("DataSource");
elemDataSource.InnerText = builder.DataSource.ToString();
root.ReplaceChild(elemDataSource, root.ChildNodes[0]);
//Replace the InitialCatalog Element Of XML Document
XmlElement elemSInitialCatalog = doc.CreateElement("InitialCatalog");
elemSInitialCatalog.InnerText = builder.InitialCatalog.ToString();
root.ReplaceChild(elemSInitialCatalog, root.ChildNodes[1]);
//Replace the UserID Element Of XML Document
XmlElement elemUserID = doc.CreateElement("UserID");
elemUserID.InnerText = builder.UserID;
root.ReplaceChild(elemUserID, root.ChildNodes[2]);
//Replace the Password Element Of XML Document
XmlElement elemPassword = doc.CreateElement("Password");
elemPassword.InnerText = builder.Password;
root.ReplaceChild(elemPassword, root.ChildNodes[3]);
//Replace the IntegratedSecurity Element Of XML Document
XmlElement elemIntegratedSecurity = doc.CreateElement("IntegratedSecurity");
elemIntegratedSecurity.InnerText = builder.IntegratedSecurity.ToString();
root.ReplaceChild(elemIntegratedSecurity, root.ChildNodes[4]);
//Replace the Security Element Of XML Document
XmlElement elemSecurity = doc.CreateElement("Security");
elemSecurity.InnerText = Security.ToString();
root.ReplaceChild(elemSecurity, root.ChildNodes[5]);
}
doc.Save(path);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}