1

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;                
    }        
}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
ElectricRay81
  • 121
  • 11
  • just a thought wouldn't be easier to deserialize the xml document and update the properties then serialize it again rather manipulating via XML documents. https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – coder_b Jun 23 '22 at 17:27
  • I don't know that's why this question but that wouldn't help me with adding the specific data at the right place (I think) hence it is controlled by a combobox so the user will select to which machine the data is added – ElectricRay81 Jun 23 '22 at 17:31

1 Answers1

2

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);
        }
    }
  
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Is there now a easy way to append new data to the machine which has been found. This should be a subchild. With all new information i.e. measurement data? Or shal lI create a new topic for this? – ElectricRay81 Jun 24 '22 at 14:29
  • 1
    Added line which will put new items at end of child. Value can be any object string or number. – jdweng Jun 24 '22 at 16:59
  • Probably I do something wrong but when I add the line of code as you suggested nothing happens to my xml file.... – ElectricRay81 Jun 24 '22 at 18:16
  • You need to save changes. Added line of code. – jdweng Jun 24 '22 at 20:26