Home > Mobile >  Search innertext and add data to XML file
Search innertext and add data to XML file

Time:06-25

I have an existing XML file and I have data that I want to add at a specific place into this xml file. Unfortunately I'm unable to add the data where I want. What I tried to do is first load the xml file than I have a combobox which shows certain elements in the xml file. By selecting an item in the combobox the code should know where to place the data (which comes from a datagridview all that data is placed into a class. But before appending data it seems that my code is not able to find the place where I want to add the data.

So main question is how can I search on innertext from a XML file, second how can I add on that specific place the data I want?

An example of a XML file I have is below:

<Root>
 <CompanyProfile>
 <CompanyName>ABB</CompanyName>
 <SiteName>EWP</SiteName>
 <ClientNumber>99999</ClientNumber>
 <Imo>987654</Imo>
 <MachineTotal>2</MachineTotal>
 </CompanyProfile>
 <MachineProfile>
 <MachineName>Bowthruster</MachineName>
 <SerialNumber>123456</SerialNumber>
 <TypeNumber>213654</TypeNumber>
 <Type>SKA</Type>
 <NominalSpeed>1500</NominalSpeed>
 <Frequency>50</Frequency>
 <NominalPower>15000</NominalPower>
 </MachineProfile>
 <MachineProfile>
 <MachineName>Stuurboord</MachineName>
 <SerialNumber>66666</SerialNumber>
 <TypeNumber>999987</TypeNumber>
 <Type>Synchrone</Type>
 <NominalSpeed>3000</NominalSpeed>
 <Frequency>50</Frequency>
 <NominalPower>17000</NominalPower>
 </MachineProfile>
</Root>

The code that I was trying to use for finding the correct machine and add the data is here:

private void btnDone_Click(object sender, EventArgs e)
    {
        string SelectedMachine = cmbMachineName.SelectedItem.ToString();

        XmlDocument ClientFile = new XmlDocument();                                     //Create XmlDocument
        ClientFile.Load(frmMain.ClientFileName);                                        //Load the file as selected in the main form

        XmlNodeList NodeList = ClientFile.SelectNodes("MachineName");

        foreach(XmlNode Node in NodeList)
        {
            if (Node.ToString() == SelectedMachine)
            {
                //Here I wanted to add data from the class I have created
            }
        }

    }

The data that is being assigned to the class is coded like this....and this works I have checked it via the debugger so all values and descriptions are ok ...as far I know the issue is above when I try to search for the correct place and add the data. Note that this is a part of the code I use for assignning the data this is just to show what and how I have done it. Probably there are people how say this is not the best way but that is off topic (although I ways like to learn)

XmlMachineMeasurement NewMachineMeasurement = new XmlMachineMeasurement();

        //Check for measuringpoint and add to class
        for (int i = 0; i < TotalMeasurements; i  )
        {
            MeasuringPoint = dataGridView1.Rows[i].Cells[0].Value.ToString();

            switch (MeasuringPoint)
            {
                case "H1":
                    NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    NewMachineMeasurement.RMShorDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    break;

                case "V1":
                    NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    NewMachineMeasurement.RMSvertDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    break;

                case "A1":
                    NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    NewMachineMeasurement.RMSaxDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    break;

                case "H2":
                    NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    NewMachineMeasurement.RMShorNDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    break;

                case "V2":
                    NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    NewMachineMeasurement.RMSvertNDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    break;

                case "A2":
                    NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    NewMachineMeasurement.RMSaxNDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    break;  
                   
            }        
        }

CodePudding user response:

Real simply using Xml Linq :

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication23
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string machineName = "Bowthruster";

            XDocument doc = XDocument.Load(FILENAME);

            XElement machine = doc.Root.Elements("MachineProfile").Where(x => (string)x.Element("MachineName") == machineName).FirstOrDefault();

            machine.Add(new XElement("Tag_NAME", "Value"));
            doc.Save(FILENAME);
        }
    }
  
}
  • Related