1

I am editing an existing XML file. I cannot edit an element (the element “range”) that is a multiple value element (a list or an array?), as shown in the XML code below.

    <objects>
        <PinJoint name="revolute">          
            <socket_parent_frame>torso_offset</socket_parent_frame>          
            <socket_child_frame>base_offset</socket_child_frame>          
            <coordinates>
                <Coordinate name="revolute_angle">              
                    <default_value>0</default_value>             
                    <default_speed_value>0</default_speed_value>              
                    <range>-1.5700000000000001 1.5700000000000001</range>              
                    <clamped>true</clamped>
                    <locked>true</locked>
                </Coordinate>
            </coordinates>

I want to rewrite the values of the element “range”. In this case, these are two numeric values (doubles) separated by spaces.

I am trying to do this with the following c# code

    XDocument xdoc = XDocument.Load(@"G:/My Drive/File.xml");
    double r1 = new double[,] { {0.1745, 1.3963 } };
    var pinjoints = xdoc.Root.Descendants("objects").Elements("PinJoint").Descendants("coordinates").Elements("Coordinate").FirstOrDefault(x => x.Attribute("name").Value == "revolute_angle");
    pinjoints.SetElementValue("range", r1);

but the “range” element is rewritten as follow:

    <range>System.Double[,]</range>

Can you please help me to edit the element “range” by rewriting one or all its numeric values?

Best regards

Andrés

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
Andrés
  • 21
  • 2
  • 1
    Xml values are string so use following : string r1str = string.Join(" ", new object[] { r1[0,0].ToString() ,r1[0,1].ToString()}) ; – jdweng Oct 26 '22 at 14:18

1 Answers1

1

You are passing array(r1) to the XML element as a value. XML elements accept string. You can replace your code with the following code & it should work.

    XDocument xdoc = XDocument.Load(@"G:/My Drive/File.xml");
    double[] r1 = new double[]  {0.1745, 1.3963 };
    var str = string.Join(",",r1);
    var pinjoints = xdoc.Root.Descendants("objects").Elements("PinJoint").Descendants("coordinates").Elements("Coordinate").FirstOrDefault(x => x.Attribute("name").Value == "revolute_angle");
    pinjoints.SetElementValue("range", str);

Here, I have just used string.Join() method to produce comma separated string that can be easily passed to XML element.

HarrY
  • 607
  • 4
  • 14