• 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
Question by Tekman03 · Aug 22, 2017 at 08:51 AM · errorxmlxmlserializerdeserializationstreamreader

Error while deserializing an xml file

Hi there

I am trying to use stream reader to deserialise an xml file using this code:

 var encoding = Encoding.GetEncoding("UTF-8");
         XmlSerializer serializer = new XmlSerializer(typeof(string));
         
         string filename2 = Application.dataPath + "/StreamingAssets/WordLists/wordlists.xml";
         using (StreamReader reader = new StreamReader(filename2))
         {
             wordList = serializer.Deserialize(reader) as WordLists;
             
         }

But I keep getting this error: InvalidOperationException: was not expected System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) Rethrow as InvalidOperationException: There is an error in XML document. System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader) System.Xml.Serialization.XmlSerializer.Deserialize (System.IO.TextReader textReader) CreateWordList.AddWordListToList () (at Assets/scripts/XML/CreateWordList.cs:46) CreateWordList.AddWordList () (at Assets/scripts/XML/CreateWordList.cs:35) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

Thanks in advance

Comment

People who like this

0 Show 3
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 DJT4NN3R · Aug 22, 2017 at 09:05 AM 0
Share

What is the wordList variable? I do not see it's declaration anywhere in your snippet.

avatar image Tekman03 DJT4NN3R · Aug 22, 2017 at 10:05 AM 0
Share

Sorry forgot. Its a list declared in another class linked as wordList

avatar image Tekman03 DJT4NN3R · Aug 22, 2017 at 10:06 AM 0
Share

@DJT4NN3R I am currently trying to slightly modify your code to fit, but thanks for the pointer in the right direction.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by DJT4NN3R · Aug 22, 2017 at 09:07 AM

Try this instead.

 XmlSerializer serializer = new XmlSerializer(typeof(string));
 string filename2 = Application.dataPath + "/StreamingAssets/WordLists/wordlists.xml"
 FileStream stream = new FileStream(filename2, FileMode.Open);
 XmlReader reader = XmlReader.Create(stream);

 string result;

 result = (string)serializer.Deserialize(reader);
 stream.Close();

Comment

People who like this

0 Show 5 · 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 Tekman03 · Aug 22, 2017 at 10:33 AM 1
Share

@DJT4NN3R still having problems with this. It appears to be reading it ok but then produces the same error.

avatar image DJT4NN3R Tekman03 · Aug 22, 2017 at 12:13 PM 0
Share

This was copied and pasted from my current project that's working perfectly fine. Perhaps it's a problem with your XML file...Have you made any manual changes to it?

avatar image Tekman03 · Aug 22, 2017 at 10:34 AM 0
Share

Also the page has left out the actual cause of the error: <WordLists xmlns=''> It says this was not expected in my xml file.

avatar image DJT4NN3R Tekman03 · Aug 22, 2017 at 12:12 PM 0
Share

Did removing that snippet get it to work?

avatar image Tekman03 DJT4NN3R · Aug 22, 2017 at 05:00 PM 0
Share

@DJT4NN3R no its not working yet. I'm not really sure what is wrong as I have just started to learn this!

avatar image

Answer by walidabazo · Sep 30, 2017 at 03:19 AM

You can show this video

https://youtu.be/ZF707fWAfoc

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

90 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

Related Questions

Need Help With XML Reading Class Initialization 1 Answer

I dont understand why the script to read .xml doesnt works. 1 Answer

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

[SOLVED] XML Deserialization of a single XML file into multiple objects 2 Answers

xml parsing error 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