- Home /
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!!! :)
Answer by $$anonymous$$ · Aug 14, 2011 at 08:58 AM
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 DO$$anonymous$$ way to do it. Plain XmlWriter is simpler of course. Why is it wherever I see X$$anonymous$$L, I also see over-engineering? It was once such a simple thing.
"XmlDocument" => the type or namespace name could not be found. What wrong? I just c/c your code...
Try download the full project https://sites.google.com/site/unitynoobs2/unity-noobs/xmldatawriter.zip?attredirects=0&d=1
Answer by SmoothCurves · Aug 13, 2011 at 02:31 AM
,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...
Answer by Waz · Aug 14, 2011 at 12:17 AM
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 X$$anonymous$$L 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
Your answer
Follow this Question
Related Questions
Using XML for a simple webplayer Highscore Table 1 Answer
Work with XML hidden for the user? 1 Answer
Write to XML-file within Assets\Resources 1 Answer
Editing an XML File 1 Answer
Write and Read to a iOS device! Help 1 Answer