Search Unity

XML - Writing to an existing xml file

Discussion in 'Scripting' started by Deleted User, Apr 3, 2011.

  1. Deleted User

    Deleted User

    Guest

    Hello everyone, in the last thread I showed how to read information from an existing xml file. Now I'll show you a method to write to an existing xml file and then convert the strings to float values​​. You can download the full project in my blog http://unitynoobs.blogspot.com/


    The xml file:
    Code (csharp):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <transforms>
    4.   <rotation>
    5.     <x>values</x>
    6.     <y>values</y>
    7.     <z>values</z>
    8.   </rotation>
    9. </transforms>
    10.  
    The variables:
    Code (csharp):
    1.  
    2.  public GameObject CubeObject; // object to get a apply the transforms.
    3.  
    4.  private string x = ""; // string to work with the xml file.
    5.  private string y = ""; // string to work with the xml file.
    6.  private string z = ""; // string to work with the xml file.
    7.  
    8.  private float X = 0; // we will apply the values ​​of the strings here.
    9.  private float Y = 0; // we will apply the values ​​of the strings here.
    10.  private float Z = 0; // we will apply the values ​​of the strings here.
    11.  
    12.  void Update()
    13.  {
    14.   x = CubeObject.transform.rotation.eulerAngles.x.ToString(); // taking the values ​​of transformation.
    15.   y = CubeObject.transform.rotation.eulerAngles.y.ToString(); // taking the values ​​of transformation.
    16.   z = CubeObject.transform.rotation.eulerAngles.z.ToString(); // taking the values ​​of transformation.
    17.  }
    18.  
    And the functions:
    Code (csharp):
    1.  
    2. public void WriteToXml()
    3.  {
    4.  
    5.      string filepath = Application.dataPath + @"/Data/gamexmldata.xml";
    6.   XmlDocument xmlDoc = new XmlDocument();
    7.  
    8.   if(File.Exists (filepath))
    9.   {
    10.    xmlDoc.Load(filepath);
    11.    
    12.    XmlElement elmRoot = xmlDoc.DocumentElement;
    13.    
    14.     elmRoot.RemoveAll(); // remove all inside the transforms node.
    15.    
    16.     XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node.
    17.    
    18.      XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.
    19.       rotation_X.InnerText = x; // apply to the node text the values of the variable.
    20.    
    21.      XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.
    22.       rotation_Y.InnerText = y; // apply to the node text the values of the variable.
    23.    
    24.      XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.
    25.       rotation_Z.InnerText = z; // apply to the node text the values of the variable.
    26.    
    27.    elmNew.AppendChild(rotation_X); // make the rotation node the parent.
    28.    elmNew.AppendChild(rotation_Y); // make the rotation node the parent.
    29.    elmNew.AppendChild(rotation_Z); // make the rotation node the parent.
    30.    elmRoot.AppendChild(elmNew); // make the transform node the parent.
    31.    
    32.    xmlDoc.Save(filepath); // save file.
    33.   }
    34.  }
    35.  
    Code (csharp):
    1.  
    2.  
    3. public void LoadFromXml()
    4.  {
    5.   string filepath = Application.dataPath + @"/Data/gamexmldata.xml";
    6.   XmlDocument xmlDoc = new XmlDocument();
    7.  
    8.   if(File.Exists (filepath))
    9.   {
    10.    xmlDoc.Load(filepath);
    11.    
    12.    XmlNodeList transformList = xmlDoc.GetElementsByTagName("rotation");
    13.  
    14.    foreach (XmlNode transformInfo in transformList)
    15.    {
    16.     XmlNodeList transformcontent = transformInfo.ChildNodes;
    17.    
    18.     foreach (XmlNode transformItens in transformcontent)
    19.     {
    20.      if(transformItens.Name == "x")
    21.      {
    22.       X = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the X variable.
    23.      }
    24.      if(transformItens.Name == "y")
    25.      {
    26.       Y = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Y variable.
    27.      }
    28.      if(transformItens.Name == "z")
    29.      {
    30.       Z = float.Parse(transformItens.InnerText); // convert the strings to float and apply to the Z variable.
    31.      }
    32.      
    33.     }
    34.    }
    35.   }
    36.   CubeObject.transform.eulerAngles =new Vector3(X,Y,Z); // Apply the values to the cube object.
    37.  
    38.  }
    39.  
    You can download the full project in my blog http://unitynoobs.blogspot.com/