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
1
Question by Geometrically · Apr 23, 2019 at 08:42 AM · serializationxmldata storagexmlserializer

"Root Element Missing: XML Exception" when trying to load data from XML file

Hey Unity Forums! I am trying to create a saving system for my test game in Unity, but I keep on getting this error whenever I try and load the data from the XML file:

 XmlException: Root element is missing.
 System.Xml.XmlTextReaderImpl.Throw (System.Exception e) (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo (System.String res) (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.XmlTextReaderImpl.ParseDocumentContent () (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.XmlTextReaderImpl.Read () (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.XmlTextReader.Read () (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.XmlReader.MoveToContent () (at <f9ec74c9799148aaa919695e2037e193>:0)
 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGameData.Read3_GameData () (at <41673fd343b24352b82942feb35ab8c7>:0)
 System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
 Rethrow as InvalidOperationException: There is an error in XML document (0, 0).
 System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader, System.String encodingStyle, System.Xml.Serialization.XmlDeserializationEvents events) (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader, System.String encodingStyle) (at <f9ec74c9799148aaa919695e2037e193>:0)
 System.Xml.Serialization.XmlSerializer.Deserialize (System.IO.Stream stream) (at <f9ec74c9799148aaa919695e2037e193>:0)
 DataManagement.LoadData () (at Assets/Scripts/Persistence/DataManagement.cs:60)
 LoadGameMenu+<>c__DisplayClass2_0.<Start>b__0 () (at Assets/Scripts/UI/Main Menu/LoadGameMenu.cs:29)
 UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
 UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
 UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
 UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventSystems.EventSystem:Update()
 

This is the DataManagement Class:

 using UnityEngine;
 
 using System.IO;
 using System.Xml.Serialization;
 
 public class DataManagement : MonoBehaviour
 {
     public static DataManagement dataManagement;
     public static string saveName;
 
     private string savesDirectory;
 
     private void Awake()
     {
         if (dataManagement == null)
         {
             DontDestroyOnLoad(gameObject);
             dataManagement = this;
         }
         else if (dataManagement != this)
         {
             Destroy(gameObject);
         }
 
         savesDirectory = Path.Combine(Application.persistentDataPath, "saves");
         
         if (!Directory.Exists(savesDirectory))
             Directory.CreateDirectory(savesDirectory);
     }
 
     public void SaveData(int playerLives, string currentLevel, Checkpoint lastCheckpoint)
     {
         var serializer = new XmlSerializer(typeof(GameData));
 
         var data = new GameData();
 
         data.playerLives = playerLives;
         data.currentLevel = currentLevel;
 
         data.checkpointX = lastCheckpoint.transform.position.x;
         data.checkpointY = lastCheckpoint.transform.position.y;
         data.checkpointZ = lastCheckpoint.transform.position.z;
 
         using (var stream = new FileStream(Path.Combine(savesDirectory, saveName + ".xml"), FileMode.Create))
         {
             serializer.Serialize(stream, data);
         }
     }
 
     public GameData LoadData()
     {
         string loadedFile = Path.Combine(savesDirectory, saveName + ".xml");
 
         if (File.Exists(loadedFile))
         {
             var serializer = new XmlSerializer(typeof(GameData));
             using (var stream = new FileStream(Path.Combine(savesDirectory, saveName + ".xml"), FileMode.Create))
             {
                 return serializer.Deserialize(stream) as GameData;
             }
         }
 
         throw new FileNotFoundException();
     }
 }
 
 public class GameData
 {
     public int playerLives;
     public string currentLevel;
 
     public float checkpointX;
     public float checkpointY;
     public float checkpointZ;
 }
 

And here is the Test.xml file generated by the code:

 <?xml version="1.0"?>
 <GameData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <playerLives>3</playerLives>
   <currentLevel>Level 1</currentLevel>
   <checkpointX>-6.28</checkpointX>
   <checkpointY>-0.43</checkpointY>
   <checkpointZ>4.046777</checkpointZ>
 </GameData>


Thanks for the help!

Comment
Add comment · Show 1
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 Kirki_333 · Apr 20, 2020 at 12:38 PM 0
Share

I have the same problem..did you find any solution to that?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Apr 20, 2020 at 07:39 PM

Try removing the schema attributes.

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
avatar image
0

Answer by Priyanka-Rajwanshi · Jun 28, 2020 at 10:11 AM

Try using StringReader for deserialization.

 public GameData LoadData()
 {
     if (File.Exists(filePath))
     {
         string fileText = File.ReadAllText(filePath);
         XmlSerializer serializer = new XmlSerializer(typeof(GameData));
         using (StringReader reader = new StringReader(fileText))
         {
             return (GameData)(serializer.Deserialize(reader)) as GameData;
         }
     }
     return null;
 }

See this link for more information, http://codesaying.com/parse-xml-in-unity3d/

Comment
Add comment · Show 1 · 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 Keita-kun · Aug 24, 2020 at 10:25 AM 0
Share

This link may work for simple stuff like

example

but with real world problems no use I see this link hanging around the answers here like it it a bible of serialization the article there is about parsing an existing X$$anonymous$$L file most problems where I see this link are serialization related.

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

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

Problems with the XML Serializer 1 Answer

PlayerPrefs v. XML File for Saving Data? 1 Answer

Xml Serialization of "sub classes" 0 Answers

URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer

Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers


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