我一直在嘗試在 Unity 中制作一個 C# 腳本來讀取 XML 檔案。我得到了錯誤:
XmlException: '”' 是一個意外的標記。預期的標記是 '"' 或 '''。第 1 行,位置 15。System.Xml.XmlTextReaderImpl.Throw (System.Exception e)(位于 <0f9699188f0c414ea6fb5557f5c16d15>:0)
該錯誤對我來說沒有意義,因為它指向第 1 行和超出那里的字符。似乎是針對編譯后的代碼。
這是我根據教程制作的 C# 代碼。如果它不好和不完整,很抱歉。
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;
}
}
以及它正在讀取的檔案片段。
<?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>
uj5u.com熱心網友回復:
這些行中的智能(卷曲)引號在 XML 中無效。錯誤資訊指的是第 1 行,也就是 XML 宣告,位置 15 是 中的第一個智能引號version=,但是這兩行中的所有智能引號都無效。:
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<page ID = “dlg_1”>
應該是這樣的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<page ID = "dlg_1">
為避免發生這種情況,請停止在文字處理器中撰寫或編輯 XML,而改用文本編輯器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/336654.html
上一篇:找到玩家右側/左側
