Search Unity

XML update from different objects issue

Discussion in 'Scripting' started by Heral, Aug 20, 2014.

  1. Heral

    Heral

    Joined:
    Aug 20, 2014
    Posts:
    3
    Hi guys

    I know that there are hundreds of threads talking about xml, how to load and save it, etc etc
    I open this post just to ask if someone can help me with a little problem that I'm not sure how to fix

    I have a scene where I read an xml file and update it using serialize method as I show:

    Code (CSharp):
    1. TextAsset textAsset = (TextAsset)Resources.Load(NameXml, typeof(TextAsset));
    2.         SHOWPOIMANSION1 SPM1 = new SHOWPOIMANSION1 ();
    3.         SPM1 = SPM1.Load (textAsset);
    4.  
    5.  
    6.         if (obj.name == "POI_MesaDespacho_1") {
    7.                         SPM1.ShowPOI_MesaDespacho_1.POI = 1;
    8.                 } else {
    9.                         SPM1.ShowPOI_CajaFuerte_1.POI = 1;
    10.                 }
    11.      
    12.         var serializer = new XmlSerializer (typeof(SHOWPOIMANSION1));
    13.         var stream = new FileStream (Application.dataPath + "/Resources/" + NameXml + ".xml", FileMode.Open);
    14.  
    15.         serializer.Serialize (stream, SPM1);
    16.         stream.Close ();
    17.  
    SHOWPOIMANSION1 is a class defined with some elements, one of them is "ShowPOI_MesaDespacho_1" but there are more

    My problem is that when I apply the same code for other element, doesn't keep the first change applied to xml file
    Looks like if always load the first xml file that I loaded when I started to use it

    Anyone know why append that? maybe I should close the file in some way or similar?
    Problem is that don't update correctly objects, load first xml file that existed and always that write or update something, do that against the first xml load, not to the last update

    any advise?
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  3. Heral

    Heral

    Joined:
    Aug 20, 2014
    Posts:
    3
    Thanks ffMalzbier

    I have my xml file under Assets/Resources/ directly, that is where Resources.load() search for assets
    That works properly so I'm not sure if that's the problem

    I'm going to try that about streamingAssets
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    When you build your game , the files in the resources folder will be packaged into a container file called resources.assets . You can read with Resources.Load() but you can not write to it. streamingAssets will do what you are searching for. (You can't use Resources.Load() in that case)
     
  5. Heral

    Heral

    Joined:
    Aug 20, 2014
    Posts:
    3
    StreamingAssets was final solution, thanks!!