1

I know this is a simple question but I am stuck. I have a simple WinForms application that collects the server name, username, and password and then under service credentials, the account and password. I simply want to write these values to an XML text file (not a database). I guess it should create the file if it doesn't exist. Do I need to create a root element? Can someone please help me out?

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtServerName;
private System.Windows.Forms.TextBox txtDatabaseUserName;
private System.Windows.Forms.TextBox txtDatabasePassword;
private System.Windows.Forms.TextBox txtServicePassword;
private System.Windows.Forms.TextBox txtAccount;

All I have so far is:

 private void btnSave_Click(object sender, System.EventArgs e)
        {
            string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(fileName);

            XmlNode databaseServer = doc.CreateElement("DatabaseServer");
            XmlNode databaseUserName = doc.CreateElement("DatabaseServerName");
        }
user2471435
  • 1,644
  • 7
  • 35
  • 62

2 Answers2

3

First create the XML string see this answer for many ways to do that .

For example:

XDocument doc = new XDocument();     
XElement xml = new XElement("Info",
    new XElement("Password", password),
    new XElement("UserName", userName));
doc.Add(xml);
doc.Save(path);
Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
1

This is a bit of an open ended question as you don't supply the database structure, I'd recommend though to access the DB through entity framework (google it) and then follow this guide:

Export SQL Server Database Table to XML Using Linq

Community
  • 1
  • 1
Jon Barker
  • 1,788
  • 15
  • 25