-2

I've the following structure:

<Root name="jack">
     <Ele1>4</Ele1>
     <Ele2>4</Ele2>
     <Ele3>4</Ele3>
     <Ele4>4</Ele4>
</Root>

I want to modify some values so I use the following code:

XElement root1 = new XElement("Root");
root1.SetAttributeValue("name", "jack");
root1.SetElementValue("Ele1", 1);
root1.SetElementValue("Ele2", 2);
root1.SetElementValue("Ele3", 3);

The result of this is a new structure:

<Root name="jack">
     <Ele3>3</Ele3>
</Root>

If I print root1 Debug.Log(root1)after setting the value of Ele2 the result is:

<Root name="jack">
     <Ele2>2</Ele2>
</Root>

Respectively if I do after setting the value of Ele1.

I'm using C# & MonoDevelop for Unity3D.

Blaco
  • 9
  • 3
  • 2
    Can you show the code used for printing? – Tony Stark Oct 17 '14 at 17:02
  • 2
    What are you *trying* to achieve? Please show the XML you're trying to produce... – Jon Skeet Oct 17 '14 at 17:02
  • some good reading in your spare time [XElement](http://www.dotnetperls.com/xelement) or [Exelement.SetElementValue](http://www.java2s.com/Code/CSharp/XML-LINQ/XElementSetElementValuesetsthevalueofachildelementaddsachildelementorremovesachildelement.htm) – MethodMan Oct 17 '14 at 17:02
  • It works for me (tested on csharppad.com) – Jon Skeet Oct 17 '14 at 17:05
  • 2
    Also, please just use indentation for code - when you start manually writing `` tags, the formatting is distinctly worse. – Jon Skeet Oct 17 '14 at 17:06
  • Basically, adding `Console.WriteLine(root1);` at the bottom of your code gives the output you've requested. – Jon Skeet Oct 17 '14 at 17:07

1 Answers1

0

It seems to be a problem with Unity3D because it's a very simple code. Like I said, the structure is there, so I can read first instead of modify it directly. What I've done after read root1 from the xml file:

string name;
foreach ( XElement ele in root1.Elements() ){
    name = ele.Name.LocalName;
    if ( !doNotChange.Contains (name) )
        ele.SetValue ( PlayerPrefs.GetInt( name ) );
}
        

Where doNotChange is a List of strings that contains those elements who I dont need to modify. I don't really know why SetElementValue does not work in Unity3D but SetValue does. I've reported it to Unity3D. I'll edit the post with their answer.

EDIT: Unity QA Team answered me

Hey,

We have been able to reproduce this bug and have sent it for resolution with our developers.

We highly appreciate your contribution. If you have further questions, feel free to contact us.

Community
  • 1
  • 1
Blaco
  • 9
  • 3
  • That used to be a bug in Mono a few years back. This was (supposedly) fixed. I don't know about Unity3D and the environment it provides. It may be that you run an old Mono version. – Patrice Gahide Oct 18 '14 at 13:21
  • Thanks @PatriceGahide. That could be the problem. I've just checked it and Unity3D provides Monodevelop 4.0.1 when the latest stable release is MonoDevelop 5.0.1.3. I'll try with the latest version and keep you informed. – Blaco Oct 18 '14 at 14:12
  • Note that the latest Unity3D Monodevelop could run an old version of Mono for some reason. Both can be independent. – Patrice Gahide Oct 18 '14 at 17:17