Home > Net >  XmlException: '”' is an unexpected token. The expected token is '"' or 
XmlException: '”' is an unexpected token. The expected token is '"' or 

Time:10-25

I've been trying to make a C# script in Unity that reads an XML file. I got the error:

XmlException: '”' is an unexpected token. The expected token is '"' or '''. Line 1, position 15. System.Xml.XmlTextReaderImpl.Throw (System.Exception e) (at <0f9699188f0c414ea6fb5557f5c16d15>:0)

The error doesn't make sense to me as it's directed to line 1 and at a character beyond what is there. It seems to be directed to the compiled code.

This is the C# code I made that was based off of a tutorial. So sorry if it's not good and incomplete.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;

public class XMLReader : MonoBehaviour
{
    public GameObject textBox;

    public string loadedDialogue;
    public string loadedName;
    public Sprite loadedPortrait;
    public bool loadedEnd;
    public int nextLine;

    public Text dialogueBox;
    public Text nameBox;
    public Image portrait;
    XmlDocument DialogueData;

    // Start is called before the first frame update
    void Start()
    {
        TextAsset xmlFile = Resources.Load<TextAsset>("Dialogue");
        DialogueData = new XmlDocument();
        DialogueData.LoadXml(xmlFile.text);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void UpdateDialogueBox()
    {
        LoadDialogue(nextLine);
        nameBox.text = loadedName;
        dialogueBox.text = loadedName;
        portrait.sprite = loadedPortrait;
    }

    public void LoadDialogue(int dialogueID)
    {
        XmlNode data = DialogueData.SelectSingleNode("/Dialogue/chapter_1[@page='dlg_"   dialogueID.ToString()   "']");
        if (DialogueData == null)
        {
            Debug.LogError("Couldn't locate dlg_"   dialogueID.ToString()   " in the list.");
        }

        DataLoader items = new DataLoader(data);
        items.UpdateTextData();
    }
}
class DataLoader
{
    XMLReader reader;
    public string id { get; private set; }
    public string dialogue { get; private set; }
    public string name { get; private set; }
    public bool endDialogue { get; private set; }
    public int continueDialogue { get; private set; }
    public Color textColor { get; private set; }
    public Sprite curPortrait { get; private set; }

    public DataLoader(XmlNode curDialogue)
    {
        id = curDialogue.Attributes["ID"].Value;
        name = curDialogue.Attributes["name"].InnerText;
        dialogue = curDialogue.Attributes["dialogue"].InnerText;
        endDialogue = bool.Parse(curDialogue["end"].InnerText);
        continueDialogue = int.Parse(curDialogue["continue"].InnerText);

        string pathToImage = "Portraits/"   curDialogue["portrait"].InnerText;
        curPortrait = Resources.Load<Sprite>(pathToImage);

        XmlNode colorNode = curDialogue.SelectSingleNode("color");
        float r = float.Parse(colorNode["r"].InnerText);
        float g = float.Parse(colorNode["g"].InnerText);
        float b = float.Parse(colorNode["b"].InnerText);
        float a = float.Parse(colorNode["a"].InnerText);
        
        r = NormalizeColorValue(r);
        b = NormalizeColorValue(g);
        b = NormalizeColorValue(b);
        a = NormalizeColorValue(a);

        textColor = new Color(r, g, b, a);
    }


    float NormalizeColorValue(float value)
    {
        value /= 255f;
        return value;
    }

    public void UpdateTextData()
    {
        reader.loadedDialogue = dialogue;
        reader.loadedName = name;
        reader.loadedEnd = endDialogue;
        reader.loadedPortrait = curPortrait;
        reader.nextLine = continueDialogue;
    }
}

And a snippet of the file it's reading.

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>

<chapter_1>

    <page ID = “dlg_1”>
        
        <portrait>SexyMan</portrait>
        
        <name>???</name>

        <dialogue>This is a test.</dialogue>
        
        <color>
            <r>0</r>
            <g>0</g>
            <b>0</b>
            <a>255</a>
        </color>

        <end>false</end>

        <continue>2</continue>

    </page>

CodePudding user response:

The smart (curly) quotes in these lines are invalid in XML. The error message refers to line 1, which is the XML declaration, and position 15 is the first smart quote in version=, but all the smart quotes in these two lines are invalid.:

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<page ID = “dlg_1”>

It should be this instead:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<page ID = "dlg_1">

To keep this from happening, stop writing or editing your XML in a word processor and use a text editor instead.

  • Related