?
Solved

XML File Edit

Posted on 2010-01-05
6
Medium Priority
?
928 Views
Last Modified: 2013-12-17
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
Comment
Question by:seahna
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26186029
no code attached.
0
 

Author Comment

by:seahna
ID: 26186043
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
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26186062
You want us to fill in the:  Button Click Events ?

Please attach a xml structure
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 26186484
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
 

Author Comment

by:seahna
ID: 26191465
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 26191677
Not a problem, always glad to help.  ;=)
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question