After Copying XML node from one document to another document the values after decimal point is lost

ram_rocks

Active member
Joined
Jun 14, 2021
Messages
26
Programming Experience
1-3
Hello All,
I am facing a awkward behavior in C# and I really would appreciate your help.
I have a XML file 1 (Input file) and copying some nodes to another Xml file so after copied the .0 is lost
for eg: if I have value (innertext) 1.0 then after copied it changed to 1 (I know like 1.0 and 1 are same but I need that .0 too

Xml input:
XML:
<TIMING>
   <CYCLIC-TIMING>
      <OFFSET>
         <VALUE>0.0</VALUE>
      </OFFSET>
      <OFFSET2>
         <VALUE>1.0</VALUE>
       </OFFSET2>
   </CYCLIC-TIMING>
</TIMING>

output is
C#:
<TIMING>
   <CYCLIC-TIMING>
      <OFFSET>
         <VALUE>0</VALUE>
      </OFFSET>
      <OFFSET2>
         <VALUE>1</VALUE>
       </OFFSET2>
   </CYCLIC-TIMING>
</TIMING>

and if I have 0.25 then its fine the issue is only when <number>.0.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,027
Location
Chesapeake, VA
Programming Experience
10+
How are you copying the nodes?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,027
Location
Chesapeake, VA
Programming Experience
10+
I know like 1.0 and 1 are same
Actually they are not from the point of view of a computer. 1.0 would be stored in a double, float, or decimal, while a 1 would be stored in a int or byte.
 

ram_rocks

Active member
Joined
Jun 14, 2021
Messages
26
Programming Experience
1-3
How are you copying the nodes?
C#:
file1: Input

XmlDocument f2 = new();
f2.Load(Input.xml);
XmlNodeList t1= f2.GetElementsByTagName("TIMING");

XmlDocument f2 = new();
XmlElement root= f2.CreateElement("Rootnode");
f2.AppendChild(root);

foreach (XmlNode node in t1)
{
    foreach (XmlNode cn in node .ChildNodes)
    {
        if (cn.Name == "CYCLIC-TIMING")
        {
            root.AppendChild(cn);
        }
    }
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,265
Location
Norway
Programming Experience
10+
I think you have something mixed up, the code you posted can not work:
System.ArgumentException: 'The node to be inserted is from a different document context.'
and when you fix it with ImportNode the output is correct, but different from the output you posted:
HTML:
<Rootnode>
  <CYCLIC-TIMING>
    <OFFSET>
      <VALUE>0.0</VALUE>
    </OFFSET>
    <OFFSET2>
      <VALUE>1.0</VALUE>
    </OFFSET2>
  </CYCLIC-TIMING>
</Rootnode>
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,027
Location
Chesapeake, VA
Programming Experience
10+
This also works for me. I use ImportNode() as well:
C#:
using System;
using System.Xml;

namespace SimpleCS
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = @"
<TIMING>
   <CYCLIC-TIMING>
      <OFFSET>
         <VALUE>0.0</VALUE>
      </OFFSET>
      <OFFSET2>
         <VALUE>1.0</VALUE>
       </OFFSET2>
   </CYCLIC-TIMING>
</TIMING>";

            var src = new XmlDocument();
            src.LoadXml(xml);
            var cyclicTiming = src.SelectSingleNode("//CYCLIC-TIMING");

            var dst = new XmlDocument();
            var timing = dst.CreateElement("timing");
            dst.AppendChild(timing);
            timing.AppendChild(dst.ImportNode(cyclicTiming, true));

            Console.WriteLine(dst.OuterXml);
        }
    }
}
 
Top Bottom