Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Guessing that what you want to do is remove the old XML and add the new one I think the reason this does not works is because you're creating a new element and erasing it from the root, i haven't used C# for a while but can you try this?

   public void WriteXMLFile()
{
    string _filePath = Application.dataPath + @"/Data/HV_DarkRideSettings.xml";
    XmlDocument _xmlDoc = new XmlDocument();

    if (File.Exists(_filePath))
    {
        _xmlDoc.Load(_filePath);
        _xmlDoc.RemoveAll();

        XmlNode rootNode = _xmlDoc.CreateElement("Settings");

        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);

    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.