1
Answer

Problem with updating values to .xml file

Samuel Vaesy

Samuel Vaesy

16y
2.7k
1

Hello,

Im trying to update(measurement values from serial port) data from array to .xml file. The .xml file consists of sensor nodes which have child nodes: value, description and id. Only value nodes needs to be updated.

The code I have "achieved" so far only works if there is only one <sensorX> node, and I can't figure how could I get it to update each <value> node in the file. I believe this shouldn't be too difficult, maybe I'm just missing something...

The code... looks like this at the moment, please comment...

private void xmlWrite()
        {
            System.Xml.XmlDocument xmldoc = new
            System.Xml.XmlDocument();
            int id = 0;               
            xmldoc.Load("C:\\data2.xml");           
            System.Xml.XmlNode nood = xmldoc.SelectSingleNode("sensor");
            foreach (System.Xml.XmlNode node in nood)
                {
                    if (node.Name == "value")
                    {
                        node.InnerXml = arData[id];
                        id++;
                    }
                }
            xmldoc.Save("E:\\test3.xml");
         }

I tried to add foreach-loop before the "System.Xml.XmlNode" and +id after "sensor", but it only gave always some "null reference error"...

 

The .xml file looks like this(form of the content can also be changed if eases the saving procedure):

<sensor0>
    <id>T10</id>
    <description>Temperature0</description>
    <value>0</value>
</sensor0>
<sensor1>
    <id>T11</id>
    <description>Temperature1</description>
    <value>0</value>
</sensor1>
<sensor2>
    <id>T12</id>
    <description>Temperature2</description>
    <value1>23</value1>
</sensor2>
<sensor3>
    <id>T13</id>
    <description>Temperature3</description>
    <value>0</value>
</sensor3>

Answers (1)