How to write a Xml file better?

I went to write a xml file to place the object transform, but I don’t know how to write it.

Can anyone help me?

Thanks!!! :slight_smile:

3 Answers

3

,How good are you at scripts?
A while ago I posted a how too on the pennymo
blog http://pennymo.com/2009/11/21/xml-and-unity-3d/

At that time the xml export functions where only accessible from C#, apparently you can do it all from javascript.

And remember you can’t write a local file from the web player.

And, for lots more helpful info use this google query “XML write site:unity3d.com”

–Smooth

I went to write a xml file not write the unity script...

Try this:

public void WriteToXml()  
{  
    
  string filepath = Application.dataPath + @"/StreamingAssets/gamexmldata.xml";  
  XmlDocument xmlDoc = new XmlDocument();  
    
  if(File.Exists (filepath))  
  {  
   xmlDoc.Load(filepath);  
     
   XmlElement elmRoot = xmlDoc.DocumentElement;  
     
    elmRoot.RemoveAll(); // remove all inside the transforms node.  
     
    XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node.  
     
     XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.  
      rotation_X.InnerText = x; // apply to the node text the values of the variable.  
     
     XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.  
      rotation_Y.InnerText = y; // apply to the node text the values of the variable.  
     
     XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.  
      rotation_Z.InnerText = z; // apply to the node text the values of the variable.  
     
   elmNew.AppendChild(rotation_X); // make the rotation node the parent.  
   elmNew.AppendChild(rotation_Y); // make the rotation node the parent.  
   elmNew.AppendChild(rotation_Z); // make the rotation node the parent.  
   elmRoot.AppendChild(elmNew); // make the transform node the parent.  
     
   xmlDoc.Save(filepath); // save file.  
  }  
 } 

This code takes only the values ​​of rotation, but you can create other nodes and store what you want.

Full code in http://unitynoobs.blogspot.com/

That's the DOM way to do it. Plain XmlWriter is simpler of course. Why is it wherever I see XML, I also see over-engineering? It was once such a simple thing.

Thanks! I will try it!

If you mean:

How do I write the object transform into an XML file?

then you can write (and later read) the component values:

localPosition
localRotation
localScale

you also need to set the parent, though you’ll probably derive that at read time from the XML structure.

Have you a XML file sample? I not kwon how to write it. Thanks!

Of what? Do you know how to write a float to a file? If so, consider that each of the above is just 3 or 4 floats.

Try : string NewString = OTHERFLOAT.ToString(); And then try to convert it back to float

@andrew-lukasik this is my function and when i press the button the _camera bool's come true and false in repeat the second time that i press the button it come true and it remains. i need to switch the camera through true or false to each tap. private void HandleCamera() { if (!_inputManager.SwitchCamera) return; _camera = !_camera; _mainCamera.position = _camera ? _firstPersonCamera.position : _thirdPersonCamera.position; }