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

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();

You can show this video

tutorial Unity3d used database XML - YouTube