Writing to XML issue Unity3D C#
Posted
by
N0xus
on Game Development
See other posts from Game Development
or by N0xus
Published on 2012-12-17T15:18:34Z
Indexed on
2012/12/17
17:14 UTC
Read the original article
Hit count: 466
I'm trying to create a tool using Unity to generate an XML file for use in another project. Now, please, before someone suggests I do it in something, the reason I am using Unity is that it allows me to easily port this to an iPad or other device with next to no extra development. So please. Don't suggest to use something else.
At the moment, I am using the following code to write to my XML file.
public void WriteXMLFile()
{
string _filePath = Application.dataPath + @"/Data/HV_DarkRideSettings.xml";
XmlDocument _xmlDoc = new XmlDocument();
if (File.Exists(_filePath))
{
_xmlDoc.Load(_filePath);
XmlNode rootNode = _xmlDoc.CreateElement("Settings");
// _xmlDoc.AppendChild(rootNode);
rootNode.RemoveAll();
XmlElement _cornerNode = _xmlDoc.CreateElement("Screen_Corners");
_xmlDoc.DocumentElement.PrependChild(_cornerNode);
#region Top Left Corners XYZ Values
// indent top left corner value to screen corners
XmlElement _topLeftNode = _xmlDoc.CreateElement("Top_Left");
_cornerNode.AppendChild(_topLeftNode);
// set the XYZ of the top left values
XmlElement _topLeftXNode = _xmlDoc.CreateElement("TopLeftX");
XmlElement _topLeftYNode = _xmlDoc.CreateElement("TopLeftY");
XmlElement _topLeftZNode = _xmlDoc.CreateElement("TopLeftZ");
// indent these values to the top_left value in XML
_topLeftNode.AppendChild(_topLeftXNode);
_topLeftNode.AppendChild(_topLeftYNode);
_topLeftNode.AppendChild(_topLeftZNode);
#endregion
#region Bottom Left Corners XYZ Values
// indent top left corner value to screen corners
XmlElement _bottomLeftNode = _xmlDoc.CreateElement("Bottom_Left");
_cornerNode.AppendChild(_bottomLeftNode);
// set the XYZ of the top left values
XmlElement _bottomLeftXNode = _xmlDoc.CreateElement("BottomLeftX");
XmlElement _bottomLeftYNode = _xmlDoc.CreateElement("BottomLeftY");
XmlElement _bottomLeftZNode = _xmlDoc.CreateElement("BottomLeftZ");
// indent these values to the top_left value in XML
_bottomLeftNode.AppendChild(_bottomLeftXNode);
_bottomLeftNode.AppendChild(_bottomLeftYNode);
_bottomLeftNode.AppendChild(_bottomLeftZNode);
#endregion
#region Bottom Left Corners XYZ Values
// indent top left corner value to screen corners
XmlElement _bottomRightNode = _xmlDoc.CreateElement("Bottom_Right");
_cornerNode.AppendChild(_bottomRightNode);
// set the XYZ of the top left values
XmlElement _bottomRightXNode = _xmlDoc.CreateElement("BottomRightX");
XmlElement _bottomRightYNode = _xmlDoc.CreateElement("BottomRightY");
XmlElement _bottomRightZNode = _xmlDoc.CreateElement("BottomRightZ");
// indent these values to the top_left value in XML
_bottomRightNode.AppendChild(_bottomRightXNode);
_bottomRightNode.AppendChild(_bottomRightYNode);
_bottomRightNode.AppendChild(_bottomRightZNode);
#endregion
_xmlDoc.Save(_filePath);
}
}
This generates the following XML file:
<Settings>
<Screen_Corners>
<Top_Left>
<TopLeftX />
<TopLeftY />
<TopLeftZ />
</Top_Left>
<Bottom_Left>
<BottomLeftX />
<BottomLeftY />
<BottomLeftZ />
</Bottom_Left>
<Bottom_Right>
<BottomRightX />
<BottomRightY />
<BottomRightZ />
</Bottom_Right>
</Screen_Corners>
</Settings>
Which is exactly what I want. However, each time I press the button that has the WriteXMLFile() method attached to it, it's write the entire lot again. Like so:
<Settings>
<Screen_Corners>
<Top_Left>
<TopLeftX />
<TopLeftY />
<TopLeftZ />
</Top_Left>
<Bottom_Left>
<BottomLeftX />
<BottomLeftY />
<BottomLeftZ />
</Bottom_Left>
<Bottom_Right>
<BottomRightX />
<BottomRightY />
<BottomRightZ />
</Bottom_Right>
</Screen_Corners>
<Screen_Corners>
<Top_Left>
<TopLeftX />
<TopLeftY />
<TopLeftZ />
</Top_Left>
<Bottom_Left>
<BottomLeftX />
<BottomLeftY />
<BottomLeftZ />
</Bottom_Left>
<Bottom_Right>
<BottomRightX />
<BottomRightY />
<BottomRightZ />
</Bottom_Right>
</Screen_Corners>
<Screen_Corners>
<Top_Left>
<TopLeftX />
<TopLeftY />
<TopLeftZ />
</Top_Left>
<Bottom_Left>
<BottomLeftX />
<BottomLeftY />
<BottomLeftZ />
</Bottom_Left>
<Bottom_Right>
<BottomRightX />
<BottomRightY />
<BottomRightZ />
</Bottom_Right>
</Screen_Corners>
</Settings>
Now, I've written XML files before using winforms, and the WriteXMLFile function is exactly the same. However, in my winform, no matter how much I press the WriteXMLFile button, it doesn't write the whole lot again.
Is there something that I'm doing wrong or should change to stop this from happening?
© Game Development or respective owner