- Home /
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
What is the wordList
variable? I do not see it's declaration anywhere in your snippet.
Sorry forgot. Its a list declared in another class linked as wordList
@DJT4NN3R I am currently trying to slightly modify your code to fit, but thanks for the pointer in the right direction.
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();
@DJT4NN3R still having problems with this. It appears to be reading it ok but then produces the same error.
This was copied and pasted from my current project that's working perfectly fine. Perhaps it's a problem with your X$$anonymous$$L file...Have you made any manual changes to it?
Also the page has left out the actual cause of the error: <WordLists xmlns=''>
It says this was not expected in my xml file.
@DJT4NN3R no its not working yet. I'm not really sure what is wrong as I have just started to learn this!
Your answer
Follow this Question
Related Questions
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
Need Help With XML Reading Class Initialization 1 Answer
I dont understand why the script to read .xml doesnt works. 1 Answer
build error: error CS1061: Type System.IO.DirectoryInfo 'does not Contain a definition ...' 0 Answers