Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Tramplex · Oct 27, 2017 at 11:47 AM · xmlparsingparsing error

Problem with XML parse

I am saving my game dat to xml files and i have problem with one code that throws this error XmlException: Text node cannot appear in this state. Line 1, position 1. Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () Mono.Xml.XmlFilterReader.Read () System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options) System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options) System.Xml.Linq.XDocument.Load (System.IO.TextReader reader, LoadOptions options) System.Xml.Linq.XDocument.Parse (System.String s, LoadOptions options) System.Xml.Linq.XDocument.Parse (System.String s) SaveAndLoadSystem.LoadChunkState () (at Assets/scripts/SaveAndLoadSystem/SaveAndLoadSystem.cs:155) GameManager.OnLevelWasLoaded () (at Assets/scripts/GameManager.cs:93) when i try to parse file. The code is:

 public void SaveChunkState(){
         if (!System.IO.Directory.Exists(path + "/Chunks")) {
             System.IO.Directory.CreateDirectory(path + "/Chunks");
         }
         if (File.Exists (path + "/Chunks/X" + manager.playerPosition.x.ToString() + "Y" + manager.playerPosition.y.ToString() +".xml")) {
             File.Delete (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml");
         }
         XElement save = new XElement ("save");
         XElement itemsOnGround = new XElement ("itemsOnGround");
         ItemOnGround[] items = GameObject.FindObjectsOfType<ItemOnGround> ();
         foreach (ItemOnGround item in items) {
             XAttribute name = new XAttribute ("name", item.itemName);
             XAttribute posX = new XAttribute ("posX", item.gameObject.transform.position.x.ToString ());
             XAttribute posY = new XAttribute ("posY", item.gameObject.transform.position.y.ToString ());
             XAttribute posZ = new XAttribute ("posZ", item.gameObject.transform.position.z.ToString ());
             XAttribute currentFill = new XAttribute ("currentFill", item.currentFill.ToString ());
             XAttribute state = new XAttribute ("state", item.state.ToString ());
             XAttribute stack = new XAttribute ("stack", item.stack.ToString ());
             XElement itemm = new XElement("item", name, posX, posY, posZ, currentFill, state, stack);
             itemsOnGround.Add (itemm);
         }
         save.Add (itemsOnGround);
         LootBox[] lootBoxes = GameObject.FindObjectsOfType<LootBox> ();
         XElement allLootBoxes = new XElement ("allLootboxes");
         foreach (LootBox lootBox in lootBoxes) {
             XElement lootBoxElement = new XElement("lootBox");
             XElement lootBoxName = new XElement ("name", lootBox.lootBoxName);
             lootBoxElement.Add (lootBoxName);
             XAttribute posX = new XAttribute ("posX", lootBox.gameObject.transform.position.x.ToString ());
             XAttribute posY = new XAttribute ("posY", lootBox.gameObject.transform.position.y.ToString ());
             XAttribute posZ = new XAttribute ("posZ", lootBox.gameObject.transform.position.z.ToString ());
             XElement lootBoxPosition = new XElement ("lootBoxPosition", posX, posY, posZ);
             lootBoxElement.Add (lootBoxPosition);
             List<ItemInInventory> itemsInLootBox = lootBox.loot;
             XElement lootBoxContainment = new XElement ("lootBoxContainment");
             foreach (ItemInInventory item in itemsInLootBox) {
                 XAttribute name = new XAttribute ("itemName", item.itemName);
                 XAttribute stac = new XAttribute ("stack", item.stack.ToString());
                 XAttribute state = new XAttribute ("state", item.state.ToString ());
                 XAttribute currentFill = new XAttribute ("currentFill", item.currentFill.ToString ());
                 XElement lootItem = new XElement("itemInLootBox", name, stac, state, currentFill);
                 lootBoxContainment.Add (lootItem);
             }
             lootBoxElement.Add (lootBoxContainment);
             allLootBoxes.Add (lootBoxElement);
         }
         save.Add (allLootBoxes);
         ResourcesSource[] resources = GameObject.FindObjectsOfType<ResourcesSource>();
         XElement allResources = new XElement ("allResources");
         for (int i = 0; i < resources.Length; i++) {
             XAttribute posX = new XAttribute ("posX", resources[i].gameObject.transform.position.x.ToString());
             XAttribute posY = new XAttribute ("posY", resources[i].gameObject.transform.position.y.ToString());
             XAttribute posZ = new XAttribute ("posZ", resources[i].gameObject.transform.position.z.ToString());
             XAttribute rotY = new XAttribute ("rotY", resources [i].gameObject.transform.rotation.eulerAngles.y.ToString ());
             XElement resourceTransform = new XElement ("lootBoxPosition", posX, posY, posZ, rotY);
             XElement resourceLeft = new XElement ("resourceLeft", resources [i].resourceAmount.ToString ());
             XElement resourceName = new XElement ("resourceName", resources [i].gameObject.name);
             XElement resourceSource = new XElement ("resourceSource", resourceTransform, resourceLeft, resourceName);
             allResources.Add (resourceSource);
         }
         save.Add (allResources);
         File.WriteAllText (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml", save.ToString ());
     }
 
     public void LoadChunkState(){
         ItemsDatabase database = GameObject.FindObjectOfType<ItemsDatabase> ();
         database.InitiateDatabase ();
         if (File.Exists (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml")) {
             XElement save = XDocument.Parse (path + "/Chunks/X" + manager.playerPosition.x.ToString () + "Y" + manager.playerPosition.y.ToString () + ".xml").Element ("save");
             foreach(XElement itemEl in save.Element("itemsOnGround").Elements("item")){
                 string itemName = itemEl.Attribute ("name").Value;
                 float posX = float.Parse (itemEl.Attribute ("posX").Value);
                 float posY = float.Parse (itemEl.Attribute ("posY").Value);
                 float posZ = float.Parse (itemEl.Attribute ("posZ").Value);
                 float currentFill = float.Parse (itemEl.Attribute ("currentFill").Value);
                 float state = float.Parse (itemEl.Attribute ("state").Value);
                 int stack = int.Parse(itemEl.Attribute ("stack").Value);
                 GameObject item = Instantiate (Resources.Load<GameObject> ("Items/" + itemName), new Vector3 (posX, posY, posZ), Quaternion.identity);
                 ItemOnGround itemOnGround = item.GetComponent<ItemOnGround> ();
                 itemOnGround.currentFill = currentFill;
                 itemOnGround.state = state;
                 itemOnGround.stack = stack;
             }
             foreach (XElement lootBoxEl in save.Element("allLootBoxes").Elements("lootBox")) {
                 string lootBoxName = lootBoxEl.Element ("name").Value;
                 float posX = float.Parse (lootBoxEl.Attribute ("posX").Value);
                 float posY = float.Parse (lootBoxEl.Attribute ("posY").Value);
                 float posZ = float.Parse (lootBoxEl.Attribute ("posZ").Value);
                 GameObject lootBox = Instantiate (Resources.Load<GameObject> ("LootBoxes/" + lootBoxName), new Vector3 (posX, posY, posZ), Quaternion.identity);
                 foreach (XElement item in lootBoxEl.Element("lootBoxContainment").Elements("itemInLootBox")) {
                     string itemName = item.Attribute ("itemName").Value;
                     int stack = int.Parse (item.Attribute ("stack").Value);
                     float state = float.Parse (item.Attribute ("state").Value);
                     float currentFill = float.Parse (item.Attribute ("currentFill").Value);
                     LootBox lootBoxCont = lootBox.GetComponent<LootBox> ();
                     ItemInInventory addedItem = database.items [itemName].MakeCopy();
                     addedItem.state = state;
                     addedItem.currentFill = currentFill;
                     addedItem.stack = stack;
                     addedItem.isEquiped = false;
                     lootBoxCont.loot.Add (addedItem);
                 }
             }
             foreach (XElement resourceSource in save.Element("allResources").Elements("resourceSource")) {
                 Transform resourceSpawner = GameObject.Find ("ResourcesSpawner").transform;
                 string resourceName = resourceSource.Element ("resourceName").Value;
                 float posX = float.Parse (resourceSource.Element("resourceTransform").Attribute ("posX").Value);
                 float posY = float.Parse (resourceSource.Element("resourceTransform").Attribute ("posY").Value);
                 float posZ = float.Parse (resourceSource.Element("resourceTransform").Attribute ("posZ").Value);
                 float rotY = float.Parse(resourceSource.Element("resourceTransform").Attribute ("rotY").Value);
                 Quaternion resourceRotation = new Quaternion ();
                 resourceRotation.eulerAngles = new Vector3 (0f, rotY, 0f);
                 GameObject resource = Instantiate (Resources.Load<GameObject> ("/ResourceSources" + resourceName), new Vector3 (posX, posY, posZ), resourceRotation, resourceSpawner);
                 ResourcesSource resSource = resource.GetComponent<ResourcesSource> ();
                 resSource.resourceAmount = int.Parse (resourceSource.Attribute ("resourceLeft").Value);
             }
         }
     }

And it is not working. BUT i am saving other data EXACTLY the same way and it works fine and is beeing loaded fine. What the problem here?

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 Bunny83 · Oct 27, 2017 at 12:28 PM

XDocument.Parse expects an XML string, not a path name. (Line 69 in your posted code).

You might want to use XDocument.Load which does take a path

Comment
Add comment · 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

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

72 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 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 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

i am pretty sure that this is right but..... 1 Answer

Parsing error 1 Answer

Parseing Varible names from strings 1 Answer

Retrive value from xml 1 Answer

Parse xml with unknown fields 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