Question : XML File Edit

Ok per my perior discussion I am reading in information from an XML document and displying it inside a text field through a Window.Form application. Now what I would like to be able to do is edit these text fields and save it to the XML file. Attached is the code that I have so far.

Answer : XML File Edit

Hi; seahna;

I rewrote the program file and you should be able just to copy and past the whole file and try it. You will need to put the XML file in the bin/debug directory of the project folder and needs to be called Config.xml or you will need to change the name in the program to match what you have at three locations.I ran this on my system and it worked without any problem.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Security.Principal;
using System.Xml;
using Stentofon.AlphaCom.AlphaNet.Client;
using Stentofon.AlphaCom.Data.State;

namespace AV4._1_ClientTool.Dialogs1.Configurations
{
    /*
     * The IP Address and Port # are needed when creating an AlphaNetClient object. This AlphaNetClient 
     * construct takes the IP Address and Port# and after assigning them to global variables, those 
     * variables are used when performing a TCP Client Connect() call. It is at this point the connection 
     * is made between the API and the exchange.
     */

    public partial class ServerConfigurations : Form
    {
        //private AlphaComState _stateStorage;
        //private AlphaNetClient _alphaNetClient;

        public ServerConfigurations()
        {
            InitializeComponent();
        }

        #region Text Field Display

        // I am assuming this is the form load event
        public void PopulateServerForm(object sender, EventArgs e)
        {
            // XElement.Load("File name with path") loads the XML file into myDoc
            // File name without path, file gets place where the exe file is
            // located, should be bin/debug in the project folder
            XElement myDoc = XElement.Load("Config.xml");

            // Fill the text boxes with the values from the XML file
            IPaddressBox.Text = myDoc.Descendants("IPAddress").FirstOrDefault().Value;
            IPportBox.Text = myDoc.Descendants("IPPort").FirstOrDefault().Value;
            ConfigPathBox.Text = myDoc.Descendants("ConfigurationPath").FirstOrDefault().Value;
        }


        #endregion Text Field Display

        #region Buttons
        private void evOKPS1_Click(object sender, EventArgs e)
        {//upon Ok button pressed stores IP Address and IP Port then closes window

            string XmlFile = "Config.xml";
            XElement myDoc = XElement.Load(XmlFile);

            // Save the values to the XML file from the text boxes
            myDoc.Descendants("IPAddress").FirstOrDefault().Value = IPaddressBox.Text;
            myDoc.Descendants("IPPort").FirstOrDefault().Value = IPportBox.Text;
            myDoc.Descendants("ConfigurationPath").FirstOrDefault().Value = ConfigPathBox.Text;

            myDoc.Save(XmlFile);

            this.Hide();
        }

        private void evApplyPS1_Click(object sender, EventArgs e)
        {//instantly stores IP Address and IP Port but does not close window
            //Store & Apply IPaddressBox, IPportBox, ConfigPathBox

            string XmlFile = "Config.xml";
            XElement myDoc = XElement.Load(XmlFile);

            // Save the values to the XML file from the text boxes
            myDoc.Descendants("IPAddress").FirstOrDefault().Value = IPaddressBox.Text;
            myDoc.Descendants("IPPort").FirstOrDefault().Value = IPportBox.Text;
            myDoc.Descendants("ConfigurationPath").FirstOrDefault().Value = ConfigPathBox.Text;

            myDoc.Save(XmlFile);

        }

        private void evCancelPS1_Click(object sender, EventArgs e)
        {//Closes the window without saving or editing information
            this.Close();
        }
        #endregion Buttons


    }
}
Random Solutions  
 
programming4us programming4us