Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mad_Fox · Oct 10, 2014 at 09:31 PM · filexml

Problems writing to existing xml

Hi! i followed some tutorials regarding XML files and im having my issues writing to an existing xml, my xml is something like this (real one have more items)

 <?xml version="1.0" encoding="utf-8"?>
 <levels>
         <name>Level 1-1</name>
     <title>This is level 1</title>
     </level>
     <name>Level 1-2</name>
         <title>This is level 2</title>
     </level>
 </levels>

I've a script to load this xml and put it's data to some variables so i can use ingame, this is the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;
 using System.Xml;
 using System.IO;
 
 
 public class LoadXmlData : MonoBehaviour // the Class
 {
     public int actualLevel = 1;
     static int LevelMaxNumber;
     static int WaipointCounter = 0;
     
     public static string lvlname = "";
     public static string lvltitle = "";
     
     public TextAsset XMLData;
     
     List<Dictionary<string,string>> levels = new List<Dictionary<string,string>>();
     Dictionary<string,string> obj;
     
     
     void Start()
     {    //Timeline of the Level creator
         GetLevel();
         StartCoroutine(LevelLoadInfo(0.0F));
         LevelMaxNumber = levels.Count;
     }
     
     public void GetLevel()
     {
         XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
         xmlDoc.LoadXml(XMLData.text); // load the file.
         XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level"); // array of the level nodes.
     
         foreach (XmlNode levelInfo in levelsList)
         {
             XmlNodeList levelcontent = levelInfo.ChildNodes;
             obj = new Dictionary<string,string>(); // Create a object(Dictionary) to colect the both nodes inside the level node and then put into levels[] array.
             
             foreach (XmlNode levelsItens in levelcontent) // levels itens nodes.
             {
                 if(levelsItens.Name == "name")
                 {
                     obj.Add("name",levelsItens.InnerText); // put this in the dictionary.
                 }
 
 
                 if(levelsItens.Name == "title")
                 {
                     obj.Add("title",levelsItens.InnerText); // put this in the dictionary.
                 }
 
                 
             }
             levels.Add(obj); // add whole obj dictionary in the levels[].
         }
     }
     
 
     IEnumerator LevelLoadInfo(float Wait)
     {
         levels[actualLevel-1].TryGetValue("name",out lvlname);
 
         levels[actualLevel-1].TryGetValue("title",out lvltitle);
 
         yield return new WaitForSeconds(Wait);
 
     }
 
 
     void Update()
     {
     }
 
 }

ok, everything works fine, but i would like to know how i could change some data and put it back to the xml file and save it, for example, how to change the level 2 title, i tried a lot of different things but none work at all, can i access the node directly?, do i have to loop every node and find the one a need like in the load script? how is the way to do it? thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zharik86 · Oct 11, 2014 at 07:39 AM

First, add some tag in your xml in question. Second, if you want to rewrite data in a XML file, the simplest way, it to create a text line, and then to save it in the file or somewhere else. I will write function of saving of your data:

  public void myReSave() {
   //create string as xml
   string tempStr = "";
   //Fill variable
   tempStr = tempStr + "<levels>";
   //Your all data level in dictionaries, change title of level 2 and save all dictionaries
   (levels[1])["title"] = "MegaTitle Level 2"; //change title
   for(int i = 0; i < levels.Count; i++) {
    //Create one level data
    tempStr = tempStr + "<level>";
    tempStr = tempStr + "<name>" + (levels[i])["name"] + "</name>";
    tempStr = tempStr + "<title>" + (levels[i])["title"] + "</title>";
    tempStr = tempStr + "</level>";
   }
   tempStr = tempStr + "</levels>";
   //And save your string into your file.
   //Simple way, but remember, that filepath shall exist already, differently there will be an error
   System.IO.File.WriteAllText("C:\yourfilepath\yourtextfile.xml", tempStr);
  }

I hope that it will help you.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mad_Fox · Oct 11, 2014 at 05:30 PM 0
Share

thanks for the reply zharik, but my xml is bigger than the example i posted here, and i want to know how to overwrite information using the xmlDocument procedure like in my Load function

avatar image zharik86 · Oct 11, 2014 at 06:46 PM 0
Share

@$$anonymous$$ad_Fox Are you want to get access to part of the file and to rewrite it?

avatar image Mad_Fox · Oct 12, 2014 at 02:29 AM 0
Share

@zharik86, i trya to load level info from the xml, put into variables and use ingame, that part works perfect, then, those variables are updated ingame, i try to put back those modified variables into their respective nodes in the xml document and then save the file. I dont know how to do that.

avatar image zharik86 · Oct 12, 2014 at 09:28 AM 0
Share

@$$anonymous$$ad_Fox Write your comments as comment, not answer(button "Add comment" below answer). For your case see this links: unitygems.com/xml and unitynoobs.blogspot.nl/2011/04/xml-writing-to-existing-xml-file.html?m=1.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Getting only the Name of the XML File and not the Directory of the XML File 2 Answers

Best method to save a multi-dimensional array as a file? 1 Answer

Why XmlTextReader become so slowly in Unity? 0 Answers

Android and XML (Reading/Wirting) 1 Answer

Writing to files in bytes. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges