【发布时间】:2021-12-10 00:30:39
【问题描述】:
我一直在尝试在 Unity 中制作一个读取 XML 文件的 C# 脚本。我得到了错误:
XmlException: '”' 是一个意外的标记。预期的标记是 '"' 或 '''。第 1 行,第 15 位。 System.Xml.XmlTextReaderImpl.Throw (System.Exception e) (在 :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;
}
}
还有它正在读取的文件的 sn-p。
<?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>
【问题讨论】:
-
那些花引号是无效的。
-
一般来说,您不应在 Word 等编辑器中键入 XML。