• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

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.
0
seahna
Asked:
seahna
  • 2
  • 2
  • 2
1 Solution
 
zadeveloperCommented:
no code attached.
0
 
seahnaAuthor Commented:
Ok sorry I thought it attached try this again...
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();
        }

        //read the XML file 
        private static string ConfigFileName = ConfigurationManager.AppSettings["ConfigFileName"];

        #region Text Field Display

        public void PopulateServerForm(object sender, EventArgs e)
        {
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(ConfigFileName);
            string ipAddressXpath = string.Format("Setup/ACSP/Primary/IPAddress");
            string ipPortXpath = string.Format("Setup/ACSP/Primary/IPPort");
            string configPathXpath = string.Format("Setup/ACSP/Primary/ConfigurationPath");
            IPaddressBox.Text = ReadValueFromXmlDocumentAtXPath(myDoc, ipAddressXpath);
            IPportBox.Text = ReadValueFromXmlDocumentAtXPath(myDoc, ipPortXpath);
            ConfigPathBox.Text = ReadValueFromXmlDocumentAtXPath(myDoc, configPathXpath);
        }

        public string ReadValueFromXmlDocumentAtXPath(XmlDocument myDoc, string xpath)
        {
            string value = string.Empty;
            XmlNode myNode = myDoc.SelectSingleNode(xpath);
            if (myNode != null && myNode.InnerText != null)
            {
                value = myNode.InnerText;
            }
            return 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

            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
     }

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

Open in new window

0
 
zadeveloperCommented:
You want us to fill in the:  Button Click Events ?

Please attach a xml structure
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Fernando SotoRetiredCommented:
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
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


    }
}

Open in new window

0
 
seahnaAuthor Commented:
FernandoSoto...Thank you so very much you are my life saver. This works perfectly and it provides me the basic edit structure I need for other parts of my application when I get to them.
0
 
Fernando SotoRetiredCommented:
Not a problem, always glad to help.  ;=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now